Reputation: 1
I am writing a very simplistic code to find the factorial of a large number. Essentially, since the int definition causes the overall result to overflow, I am using arrays to store individual values. I am then using addition rather than multiplication to add the individual values of the arrays in their specific columns.
The code basically reduces 100 x 99, to 'add 100 to itself 99 times.'
However, it seems to work initially, during the first cycles, but after increasing the number of cycles, I get an array of zeros as a result.
I'm sorry for the unstructured code, but I was tinkering and trying to fix it on the go, so there is not much logical cohesion.
Here is the code:
#include <iostream>
using namespace std;
void printFactorial(int array[], int);
void findFactorial()
{
int initialValue; // Value that will store 100
int num; // 100 - 1 for calculation purposes
int temp; // Stores the initial value temporarily for calculations
long int array1[180]; // This array will hold the total result
long int array2[180]; // This array will change to add to the total result num times
initialValue = 100;
num = initialValue - 1;
temp = initialValue;
for (int i = 0; i <= 170; i++) // Sets the int 100 into the array, with each digit in the corresponding position
{
array2[i] = temp % 10;
temp = temp / 10;
}
while (initialValue != 0) // While loop for recursion
{
for (int i = 1; i <= num; i++) // This causes each digit position to add to itself num times, which is equivalent to
{ // multiplication by num. What I will later do is separate the total sum into its respective
for (int j = 0; j <= 157; j++) // digits position. Say array[2] = 15, I will make array[2] = 5 and add one to array[3].
{
array1[j] += array2[j]; // This causes each digit to add to itself, and store the result in the 'total' array.
}
}
memcpy(array2, array1, sizeof(array1)); // Makes the contents of array2 that of array1, so as to make the next cycle of multiplication
// work.
initialValue--; // Decrease by one since the process must only run 100 times, and then exiting the while loop.
num--;
}
for (int i = 0; i <= 157; i++)
{
cout << array1[i] << " "; // Printing the resultant array.
}
}
int main()
{
findFactorial();
}
Upvotes: 0
Views: 108
Reputation: 5321
You never initialized array1, so it is only by accident that it started out zero (which you seem to rely on).
You never propagate any overflow from each element of array1 into the next element, which is the main problem resulting in them reaching zero through overflow.
Since you seem to intend each position to hold just one digit, you don't need the staggering inefficiency of multiplying by repeated addition. An int out to be big enough to hold one digit multiplied by your multiplier (whenever the multiplier is small enough for the whole array to hold the factorial result).
Consider an inner loop similar to:
temp += num * array[i];
array[i] = temp % 10;
temp /= 10;
Then you don't need two arrays, and each array element only needs to ever hold one digit. And temp only needs to be big enough to hold slightly more than 9*num (for the max num).
I don't want to give you more detail than that, because I assume you are doing this for the exercise of doing it yourself. Otherwise you would just use an existing library.
Upvotes: 1