large_factorial.cpp File Reference

Compute factorial of any arbitratily large number/. More...

`#include <cstring>`
`#include <ctime>`
`#include <iostream>`
`#include "./large_number.h"`
Include dependency graph for large_factorial.cpp:

## Functions

bool | test1 ()
bool | test2 ()
int | main (int argc, char *argv[])

## Detailed Description

Compute factorial of any arbitratily large number/.

Author
  Krishna Vedala

See also
  factorial.cpp

## Function Documentation

## main()

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Main program

{
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;
}
std::atoi
T atoi(T... args)
std::cin
std::cout
large_number
Definition: large_number.h:24
std::clock
T clock(T... args)
std::clock_t
std::endl
T endl(T... args)
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::scientific
T scientific(T... args)
test1
bool test1()
Definition: large_factorial.cpp:17
test2
bool test2()
Definition: large_factorial.cpp:54
Here is the call graph for this function:

## test1()

bool test1 | ( |  | ) |

Test implementation for 10! Result must be 3628800.

Returns
  True if test pass else False

{
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;
}
std::strlen
T strlen(T... args)
Here is the call graph for this function:

## test2()

bool test2 | ( |  | ) |

Test implementation for 100! The result is the 156 digit number:

9332621544394415268169923885626670049071596826438162146859296389521759
9993229915608941463976156518286253697920827223758251185210916864000000
000000000000000000

Returns
  True if test pass else False

{
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;
}
Here is the call graph for this function: