I am working on a problem from CodeChef where I need to calculate the factorial of n numbers.
The user inputs a number which determines how many ints to perform a factorial calculation on and then inputs the numbers to calculate.
My problem is with the multiplication itself. For example if I have an int == 5 then the result will be 20 (it will calculate n by the last factorial only, not by all of them)
Here is where the problem exists:
for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index
for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x
_result[x] = _numbersToProcess[x] * y;// Multiply x by y then add to array
}
}
The outer loop defines how many calculations to perform.
The inner loop calulates the factorials by iterating through each index of _numberToProcess
and multiplying it by every number less than the number to be calculated on.
The problem is that the factorial calculation overwrites itself,
for example:
factorial of 5 result: 20
but it should be 120 (it overwrites itself until it reaches the last multiplier)
So I tried the following:
_result[x] = _numbersToProcess[x] *= y;
This is obviously the same as _numbersToProcess[x] = _numbersToProcess[x] * y;
But this gives a completley different result:
If we again input 5 then this will result in the output of -1899959296.
I know I can easily copy and paste from other submissions but I want to know why my method does not result in the correct output.
Here is the method in its entirety:
int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate
int[] _numbersToProcess = new int[_numbers];// Array of inputs
int[] _result = new int[_numbers];
int i = 0;
while(i < _numbersToProcess.Length) {
_numbersToProcess[i] = int.Parse(Console.ReadLine());
i++;
}
for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index
for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x
_result[x] = _numbersToProcess[x] *= y;// Multiply x by y then add to array
}
}
for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x
Console.WriteLine(_result[n]);// Write to console
}
Console.ReadLine();
int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate
int[] _numbersToProcess = new int[_numbers];// Array of inputs
int[] _result = new int[_numbers];
int i = 0;
while(i < _numbersToProcess.Length) {
_numbersToProcess[i] = int.Parse(Console.ReadLine());
i++;
}
for (int x = 0; x < _numbersToProcess.Length; x++)
{// Loop throuigh Array by index
int fact = 1;
for (int y = 1; y <= _numbersToProcess[x]; y++)
{// Y is equal to less than index x
fact = fact*y;
}
_result[x] = fact;
}
for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x
Console.WriteLine(_result[n]);// Write to console
}
Console.ReadLine();
Problem is with your inner for loop. here, you are always, overriding result array.
i.e for y=5; inner for loop executes for 5 times.
iteration -1 :
y=1,
_numbersToProcess[5]=5
_result[x]=5
iteration -2 :
y=2,
_numbersToProcess[5]=10
_result[x]=10
iteration -3 :
y=3,
_numbersToProcess[5]=30
_result[x]=30
.
.
.
.
.
thus it goes for 12 iteration as your _numbertoprocess[5] is changing and stops once it reaches less than 0 i.e -1899959296.
iteration 12:
_numbertoprocess[5] = -1899959296.
i.e you are changing numbertoprocess everytime in your inner for loop.
you can verify it by adding
Console.WriteLine(y);
Console.WriteLine(_numbersToProcess[x]);
Console.WriteLine(_result[x]);
in your inner for loop.
for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x
_result[x] = _numbersToProcess[x] *= y;// Multiply x by y then add to array
}
In loop condition y < _numberToProcess[x];
. It compare between y
and _numberToProcess[x]
array value
I think you should edit loop condition to y < x
Be lucky.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With