[ create a new paste ] login | about

Link: http://codepad.org/xnneavsw    [ raw code | output | fork ]

jiangtanghu - C++, pasted on Jan 22:
/*
Calculate the factorial of 171 
used in blog
http://www.jiangtanghu.com/blog/2011/01/22/big-numbers/
*/

#include <iostream>
using namespace std;

double factRecursive(double num);
double factNonRecursive(double num);

int main()
{
	cout<<endl;	
	cout<<"Recursive: the factorial of 170 is "<<factRecursive(170)<<endl;
	cout<<"Recursive: the factorial of 171 is "<<factRecursive(171)<<endl;
	cout<<endl;	
	
	cout<<"NonRecursive: the factorial of 170 is "<<factNonRecursive(170)<<endl;
	cout<<"NonRecursive: the factorial of 171 is "<<factNonRecursive(171)<<endl;
	cout<<endl;	

return 0;
}

double factRecursive (double num)
{
	if (num==0)
		return 1;
	else
		return num*factRecursive(num-1);
}

double factNonRecursive (double num)
{
	double fact=1;
	for (double i=2;i<=num;i++) fact *=i;
	return fact;
}


Output:
1
2
3
4
5
6
7

Recursive: the factorial of 170 is 7.25742e+306
Recursive: the factorial of 171 is inf

NonRecursive: the factorial of 170 is 7.25742e+306
NonRecursive: the factorial of 171 is inf



Create a new paste based on this one


Comments: