-
Notifications
You must be signed in to change notification settings - Fork 0
/
big-fac.c
83 lines (68 loc) · 2.38 KB
/
big-fac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*************************************************************************
* big-fac.c
*
* Project: Big-Positive-Numbers Factorial Calculator
* Author: github@engjango
*
* Description:
* This program calculates the factorial of very large numbers, capable of
* handling results with tens of thousands of digits. The number of digits
* is estimated using Stirling's approximation.
*
* Disclaimer:
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
*
************************************************************************/
#include <stdio.h>
#include <math.h>
int main() {
// Variable to store the estimated maximum number of digits
int MAX_DIGITS_NUMBER;
// Auxiliary variables
int i, j, number, dcounter, carry, tmp;
// User input
printf("Large Number Factorial Calculator\n\n");
printf("Enter a number (e.g., 1993): ");
scanf("%d", &number);
// Estimate the number of digits in the factorial using Stirling's formula
MAX_DIGITS_NUMBER = (int)(
(log(2 * M_PI) / 2 + log(number) / 2 + number * (log(number) - 1)) / log(10)
) + 1;
// Allocate an array to hold the digits of the result
char result[MAX_DIGITS_NUMBER];
// Initialize the result with the value 1 (the factorial of 0 or 1)
result[0] = 1;
// Initialize the digit counter and carry-over variable
dcounter = 1;
carry = 0;
// Print an empty line for formatting
puts("");
// Main factorial computation loop
for (i = 1; i <= number; i++) {
for (j = 0; j < dcounter; j++) {
// Multiply the current digit by i and add the carry
tmp = result[j] * i + carry;
// Update the current digit
result[j] = tmp % 10;
// Update the carry for the next digit
carry = tmp / 10;
}
// Add remaining carry digits to the result array
while (carry > 0) {
result[dcounter] = carry % 10;
carry /= 10;
dcounter++;
}
// Display progress (number of digits calculated so far)
printf("\r%d digits calculated...", dcounter);
}
// Print an empty line for formatting
puts("");
// Display the factorial result
printf("\nFactorial of %d is:\n\n", number);
for (i = dcounter - 1; i >= 0; i--) {
printf("%d", result[i]);
}
// Exit the program successfully
return 0;
}