-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathfactorial.c
79 lines (47 loc) · 1.38 KB
/
factorial.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
/*
Testing Big-Number library by calculating factorial(100) a.k.a. 100!
====================================================================
For the uninitiated:
factorial(N) := N * (N-1) * (N-2) * ... * 1
Example:
factorial(5) = 5 * 4 * 3 * 2 * 1 = 120
Validated by Python implementation of big-numbers:
--------------------------------------------------
In [1]: import math
In [2]: "%x" % math.factorial(100)
Out[]: '1b30964ec395dc24069528d54bbda40d16e966ef9a70eb21b5b2943a321cdf10391745570cca9420c6ecb3b72ed2ee8b02ea2735c61a000000000000000000000000'
... which should also be the result of this program's calculation
*/
#include <stdio.h>
#include "bn.h"
void factorial(struct bn* n, struct bn* res)
{
struct bn tmp;
/* Copy n -> tmp */
bignum_assign(&tmp, n);
/* Decrement n by one */
bignum_dec(n);
/* Begin summing products: */
while (!bignum_is_zero(n))
{
/* res = tmp * n */
bignum_mul(&tmp, n, res);
/* n -= 1 */
bignum_dec(n);
/* tmp = res */
bignum_assign(&tmp, res);
}
/* res = tmp */
bignum_assign(res, &tmp);
}
int main()
{
struct bn num;
struct bn result;
char buf[8192];
bignum_from_int(&num, 100);
factorial(&num, &result);
bignum_to_string(&result, buf, sizeof(buf));
printf("factorial(100) using bignum = %s\n", buf);
return 0;
}