Compute factorial of any arbitratily large number/.
More...
#include <cstring>
#include <ctime>
#include <iostream>
#include "./large_number.h"
Go to the source code of this file.
Compute factorial of any arbitratily large number/.
- Author
- Krishna Vedala
- See also
- factorial.cpp
Definition in file large_factorial.cpp.
◆ main()
int main |
( |
int | argc, |
|
|
char * | argv[] ) |
Main program
Definition at line 89 of file large_factorial.cpp.
89 {
90 int number, i;
91
92 if (argc == 2) {
93 number = atoi(argv[1]);
94 } else {
95 std::cout << "Enter the value of n(n starts from 0 ): ";
96 std::cin >> number;
97 }
98
100
101 std::clock_t start_time = std::clock();
102 for (i = 2; i <= number; i++)
103 result *= i;
104 std::clock_t end_time = std::clock();
105 double time_taken =
106 static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC;
107
108 std::cout << number <<
"! = " <<
result << std::endl
109 <<
"Number of digits: " <<
result.num_digits() << std::endl
110 << "Time taken: " << std::scientific << time_taken << " s"
111 << std::endl;
112
116
117 return 0;
118}
uint64_t result(uint64_t n)
◆ test1()
Test implementation for 10! Result must be 3628800.
- Returns
- True if test pass else False
Definition at line 17 of file large_factorial.cpp.
17 {
18 std::cout << "---- Check 1\t";
19 unsigned int i, number = 10;
21 for (i = 2; i <= number; i++)
22 result *= i;
23
24 const char *known_reslt = "3628800";
25
26
27 if (strlen(known_reslt) !=
result.num_digits()) {
28 std::cerr << "Result lengths dont match! " << strlen(known_reslt)
29 <<
" != " <<
result.num_digits() << std::endl;
30 return false;
31 }
32
33 const size_t N =
result.num_digits();
34 for (i = 0; i <
N; i++) {
35 if (known_reslt[i] !=
result.digit_char(i)) {
36 std::cerr << i << "^th digit mismatch! " << known_reslt[i]
37 <<
" != " <<
result.digit_char(i) << std::endl;
38 return false;
39 }
40 }
41
42 std::cout << "Passed!" << std::endl;
43 return true;
44}
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
◆ test2()
Test implementation for 100! The result is the 156 digit number:
9332621544394415268169923885626670049071596826438162146859296389521759
9993229915608941463976156518286253697920827223758251185210916864000000
000000000000000000
- Returns
- True if test pass else False
Definition at line 54 of file large_factorial.cpp.
54 {
55 std::cout << "---- Check 2\t";
56 unsigned int i, number = 100;
58 for (i = 2; i <= number; i++)
59 result *= i;
60
61 const char *known_reslt =
62 "9332621544394415268169923885626670049071596826438162146859296389521759"
63 "9993229915608941463976156518286253697920827223758251185210916864000000"
64 "000000000000000000";
65
66
67 if (strlen(known_reslt) !=
result.num_digits()) {
68 std::cerr << "Result lengths dont match! " << strlen(known_reslt)
69 <<
" != " <<
result.num_digits() << std::endl;
70 return false;
71 }
72
73 const size_t N =
result.num_digits();
74 for (i = 0; i <
N; i++) {
75 if (known_reslt[i] !=
result.digit_char(i)) {
76 std::cerr << i << "^th digit mismatch! " << known_reslt[i]
77 <<
" != " <<
result.digit_char(i) << std::endl;
78 return false;
79 }
80 }
81
82 std::cout << "Passed!" << std::endl;
83 return true;
84}