-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
large_factorial.cpp
118 lines (100 loc) · 3.31 KB
/
large_factorial.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @file
* @brief Compute factorial of any arbitratily large number/
*
* \author [Krishna Vedala](https://github.com/kvedala)
* @see factorial.cpp
*/
#include <cstring>
#include <ctime>
#include <iostream>
#include "./large_number.h"
/** Test implementation for 10! Result must be 3628800.
* @returns True if test pass else False
*/
bool test1() {
std::cout << "---- Check 1\t";
unsigned int i, number = 10;
large_number result;
for (i = 2; i <= number; i++) /* Multiply every number from 2 thru N */
result *= i;
const char *known_reslt = "3628800";
/* check 1 */
if (strlen(known_reslt) != result.num_digits()) {
std::cerr << "Result lengths dont match! " << strlen(known_reslt)
<< " != " << result.num_digits() << std::endl;
return false;
}
const size_t N = result.num_digits();
for (i = 0; i < N; i++) {
if (known_reslt[i] != result.digit_char(i)) {
std::cerr << i << "^th digit mismatch! " << known_reslt[i]
<< " != " << result.digit_char(i) << std::endl;
return false;
}
}
std::cout << "Passed!" << std::endl;
return true;
}
/** Test implementation for 100! The result is the 156 digit number:
* ```
* 9332621544394415268169923885626670049071596826438162146859296389521759
* 9993229915608941463976156518286253697920827223758251185210916864000000
* 000000000000000000
* ```
* @returns True if test pass else False
*/
bool test2() {
std::cout << "---- Check 2\t";
unsigned int i, number = 100;
large_number result;
for (i = 2; i <= number; i++) /* Multiply every number from 2 thru N */
result *= i;
const char *known_reslt =
"9332621544394415268169923885626670049071596826438162146859296389521759"
"9993229915608941463976156518286253697920827223758251185210916864000000"
"000000000000000000";
/* check 1 */
if (strlen(known_reslt) != result.num_digits()) {
std::cerr << "Result lengths dont match! " << strlen(known_reslt)
<< " != " << result.num_digits() << std::endl;
return false;
}
const size_t N = result.num_digits();
for (i = 0; i < N; i++) {
if (known_reslt[i] != result.digit_char(i)) {
std::cerr << i << "^th digit mismatch! " << known_reslt[i]
<< " != " << result.digit_char(i) << std::endl;
return false;
}
}
std::cout << "Passed!" << std::endl;
return true;
}
/**
* Main program
**/
int main(int argc, char *argv[]) {
int number, i;
if (argc == 2) {
number = atoi(argv[1]);
} else {
std::cout << "Enter the value of n(n starts from 0 ): ";
std::cin >> number;
}
large_number result;
std::clock_t start_time = std::clock();
for (i = 2; i <= number; i++) /* Multiply every number from 2 thru N */
result *= i;
std::clock_t end_time = std::clock();
double time_taken =
static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC;
std::cout << number << "! = " << result << std::endl
<< "Number of digits: " << result.num_digits() << std::endl
<< "Time taken: " << std::scientific << time_taken << " s"
<< std::endl;
test1();
test2();
result.test();
return 0;
}