Extra long factorials in C
0 230
Extra long factorials in C are computed for integers that exceed the range of standard data types like int or long.
To compute these large factorials, specialized techniques or libraries like GMP (GNU Multiple Precision Arithmetic Library) are typically used.
Using Arrays
In this example, we use an array to store digits of the factorial and multiply digit by digit to compute the factorial.
// Program for Extra Long Factorial in C #include<stdio.h>#define MAX 500 void multiply(int x, int res[], int *res_size) { int carry = 0; for (int i = 0; i < *res_size; i++) { int prod = res[i] * x + carry; res[i] = prod % 10; carry = prod / 10; } while (carry) { res[*res_size] = carry % 10; carry = carry / 10; (*res_size)++; } } void calculateExtraLongFactorial(int n) { int res[MAX]; res[0] = 1; int res_size = 1; for (int x = 2; x <= n; x++) { multiply(x, res, &res_size); } printf("Factorial of %d is:\n", n); for (int i = res_size - 1; i >= 0; i--) { printf("%d", res[i]); } printf("\n"); } int main() { int num = 100; // Calculate factorial of 100 calculateExtraLongFactorial(num); return 0; }
Output:
Factorial of 100 is: 933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640
Example:
This C program calculates the factorial of a given integer using an array-based method, allowing it to handle large factorials that exceed the range of standard data types.
Input:
User inputs an integer value (n) for which the factorial will be calculated.
Initialization:
j starts at 2, representing the beginning of the factorial sequence.
a is a 1000-element array initialized with zeros, used to store the factorial's digits.
len begins at 1, tracking the current length of the factorial digits in the array.
Factorial Calculation:
A while loop iterates from j=2 up to n.
Inside this loop, another while loop multiplies each digit in a by j, adding any carry-over from previous multiplications.
The result is stored back in a, and any new carry-over is computed.
After multiplying and adding all digits, any remaining carry is appended to a.
Printing Result:
The factorial digits stored in a are printed in reverse order to display the final factorial value.
for n = 5:
j starts at 2, and a[0] is initialized to 1.
The program calculates 5! and stores the result in a.
After computation, a contains [1, 2, 0], which represents 120.
The program prints 120 as the factorial of 5.
Program for Extra Long Factorial in C:
// Program for Extra long Factorial in C #includeint main() { int n; printf("Enter the value for calculating factorial="); scanf("%d", &n); int j = 2; int a[1000] = {0}; a[0] = 1; int len = 1; int c = 0, num = 0; while(j <= n) { c = 0; num = 0; while(c < len) { a[c] = a[c] * j + num; num = a[c] / 10; a[c] = a[c] % 10; c++; } while(num != 0) { a[len] = num % 10; num = num / 10; len++; } j++; } len--; printf("\n\nFactorial of %d is below = ", n); while(len >= 0) { printf("%d", a[len]); len--; } return 0; }
Output:
Enter the value for calculating factorial=4 Factorial of 4 is below = 24
This program uses an array of size 1000 to accommodate large factorials.
While effective for factorials up to a certain size, this method may not be the most efficient for extremely large factorials.
Specialized libraries or algorithms can be more efficient in those cases.
Share:
Comments
Waiting for your comments