#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>

using namespace std;
unsigned __int64 Fact(unsigned __int64 x );


int main(){

	/*n! means n  (n  1)  ...  3  2  1

Find the sum of the digits in the number 100!*/


cout<<Fact(100);


}
unsigned __int64 Fact(unsigned __int64 x )
{
	__int64 num(0);

	if(x==0)
		return 1;
	else
	{
	 num = x * Fact(x-1);
	 cout<<num<<"  "<<x<<endl;
	}

	return num;
}

my recursive function works. But only for small x. (ex. 5! =120).

but when i try (100!) it runs out of space and give NULL;

so can you hint on how i can fix the code so that the computer will compute 100factorial?

OK here is a little improved version of it. Now it finds at maximum of
65!. Any hints on how to find 100!

#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>

using namespace std;
unsigned __int64 Fact(unsigned __int64 x );


int main(){

	/*n! means n  (n  1)  ...  3  2  1

Find the sum of the digits in the number 100!*/

//unsigned __int64 * fact = new unsigned __int64 (10000);

Fact(100);

}
unsigned __int64 Fact(unsigned __int64 x )
{
	unsigned __int64 * fact = new unsigned __int64 (10000);
	*fact = x;

	if(x==0)
		return 1;
	else
	{
	 *fact = x * Fact(x-1);
	 cout<<*fact<<"  "<<x<<endl;
	}

	return *fact;
	delete fact;
}

Can't you use a loop instead?

You could create a class that grows and manages the value split over vectors, overloads the general math operators you'd need, then stores the values into a file.

checked again anything over 20! is not correct. anyhelp?

Check out GMP (Gnu MultiPrecision). It allows you to work with extremely large numbers and has wrappers for c++.

To give you a clue about what's happening, 100! is about 9.3 by 10^157. To represent that, a 525 bit integer is needed (and that increases 530 bits to represent 101!). 64 bit is nowhere near enough: an unsigned 64 bit integer can only represent a maximum value of about 18446744073709551615 (approx 1.8 by 10^19).

commented: Good analysis of the underlying problem that the numbers are huge +26
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.