2020-05-02 11:12:38 +08:00
|
|
|
#include <iostream>
|
2020-05-03 03:57:12 +08:00
|
|
|
#include <string.h>
|
2020-05-02 11:12:38 +08:00
|
|
|
#include <chrono>
|
2020-05-03 03:57:12 +08:00
|
|
|
#include "large_number.h"
|
2020-05-02 11:12:38 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2020-05-02 11:31:39 +08:00
|
|
|
bool test1()
|
|
|
|
{
|
|
|
|
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 */
|
2020-05-03 03:57:12 +08:00
|
|
|
result *= i;
|
2020-05-02 11:31:39 +08:00
|
|
|
|
|
|
|
const char *known_reslt = "3628800";
|
|
|
|
|
|
|
|
/* check 1 */
|
|
|
|
if (strlen(known_reslt) != result.num_digits())
|
|
|
|
{
|
|
|
|
cerr << "Result lengths dont match! " << strlen(known_reslt) << " != " << result.num_digits() << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-03 03:57:12 +08:00
|
|
|
const size_t N = result.num_digits();
|
2020-05-02 11:31:39 +08:00
|
|
|
for (i = 0; i < N; i++)
|
|
|
|
{
|
2020-05-03 03:57:12 +08:00
|
|
|
if (known_reslt[i] != result.digit_char(i))
|
2020-05-02 11:31:39 +08:00
|
|
|
{
|
2020-05-03 03:57:12 +08:00
|
|
|
cerr << i << "^th digit mismatch! " << known_reslt[i] << " != " << result.digit_char(i) << endl;
|
2020-05-02 11:31:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Passed!" << endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test2()
|
|
|
|
{
|
|
|
|
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 */
|
2020-05-03 03:57:12 +08:00
|
|
|
result *= i;
|
2020-05-02 11:31:39 +08:00
|
|
|
|
|
|
|
const char *known_reslt = "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000";
|
|
|
|
|
|
|
|
/* check 1 */
|
|
|
|
if (strlen(known_reslt) != result.num_digits())
|
|
|
|
{
|
|
|
|
cerr << "Result lengths dont match! " << strlen(known_reslt) << " != " << result.num_digits() << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-03 03:57:12 +08:00
|
|
|
const size_t N = result.num_digits();
|
2020-05-02 11:31:39 +08:00
|
|
|
for (i = 0; i < N; i++)
|
|
|
|
{
|
2020-05-03 03:57:12 +08:00
|
|
|
if (known_reslt[i] != result.digit_char(i))
|
2020-05-02 11:31:39 +08:00
|
|
|
{
|
2020-05-03 03:57:12 +08:00
|
|
|
cerr << i << "^th digit mismatch! " << known_reslt[i] << " != " << result.digit_char(i) << endl;
|
2020-05-02 11:31:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Passed!" << endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-02 11:12:38 +08:00
|
|
|
/**
|
|
|
|
* Main program
|
|
|
|
**/
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int number, i;
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
number = atoi(argv[1]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Enter the value of n(n starts from 0 ): ");
|
|
|
|
scanf("%d", &number);
|
|
|
|
}
|
|
|
|
|
|
|
|
large_number result;
|
|
|
|
|
|
|
|
auto start_time = chrono::high_resolution_clock::now();
|
|
|
|
for (i = 2; i <= number; i++)
|
|
|
|
/* Multiply every number from 2 thru N */
|
2020-05-03 03:57:12 +08:00
|
|
|
result *= i;
|
2020-05-02 11:12:38 +08:00
|
|
|
auto end_time = chrono::high_resolution_clock::now();
|
|
|
|
chrono::duration<double> time_taken = end_time - start_time;
|
|
|
|
|
2020-05-03 03:57:12 +08:00
|
|
|
cout << number << "! = " << result << endl
|
2020-05-02 11:12:38 +08:00
|
|
|
<< "Number of digits: " << result.num_digits() << endl
|
|
|
|
<< "Time taken: " << time_taken.count() << " s"
|
|
|
|
<< endl;
|
|
|
|
|
2020-05-02 11:31:39 +08:00
|
|
|
test1();
|
|
|
|
test2();
|
2020-05-03 03:57:12 +08:00
|
|
|
result.test();
|
2020-05-02 11:31:39 +08:00
|
|
|
|
2020-05-02 11:12:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|