// Factorial.two.cpp // Use the simple (add only, non-negative) BigInt // class to calculate very large factorials. // tom bailey 17 feb 00 // Version two uses a faster multiply by adding // algorithm to speed up the calculation. // tom bailey 18 feb 00 #include using namespace std; #include "BigInt.h" #include "timer.h" int main() { short n; cout << "Enter n for n!: "; cin >> n; while( n >= 0 ) { Timer watch; watch.start(); BigInt fact( 1 ); for( short i=1; i<=n; i++ ) { BigInt sum( 0 ); BigInt factor( fact ); short mult = i; while( mult > 0 ) { if( mult%2 == 1 ) { sum += factor; mult--; } else { factor += factor; mult /= 2; } } fact = sum; } watch.stop(); cout << "factorial( " << n << " ) = " << fact; cout << endl; cout << " time required = " << watch.time(); cout << endl << endl; cout << "Enter n for n!: "; cin >> n; } return 0; }