Hi All
Can somebody give me a simple c++ program to findout n factorial, this n is any int , say 200! ,something like that.
Thanks in Adv
Printable View
Hi All
Can somebody give me a simple c++ program to findout n factorial, this n is any int , say 200! ,something like that.
Thanks in Adv
Everybody here are always willing to help, but first, you need to try. Tell us what you have done and the problem you encountered. Providing sample code would help us narrowing down the problem.:)
For your information: 200! is something about 7.88 *10^374 -- a number that does not fit well in built in integral type variables. So the main point of such a program is the definition of an integral type of arbitrary length, and of course the definition of the arithmetic operations for such a type.
Yup, nice homework, indeed...:cool:
i'm sure there is a faster, more mathematical way to do it but don't you simply loop? For today's computers, 200 iterations of simple math isn't going to slow it down much, if only done a few times ;)
Code:ULONGLONG factorial( short n )
{
ULONGLONG result = 1L;
for( short i = 1;i <= n;i++ )
result *= i;
return result;
}
Is the data type you used similar to LONGLONG type definition ?
How do you know about that type ? Oh, I mean because I have not seen it before or found it out in my textbook...
May I ask for that ?
Thank you...
Regards,
Nina
its a vc/windows datatype (like DWORD, you can tell because it is uppercase) for a unsigned 64bit integer. There is also unsigned __int64 which I believe is also vc specific.
Regards,
I was just thinking... an infinite length integer class would be cool... most 64bit+ classes work by storing in two or more 32bit variables. So, using similar logic it must be possible to build a similar class with a dynamic array of 32bit variables.
There are many arbitrary precision integer arithmetic class libraries available. I have used them alot in numerics, and yeas, they are very cool! ;)
I know people have already talked about integer ranges being too small, but the double data type is also too small to hold the value of 200! (The max double value is 1.7976931348623158e+308). Anyway, if you don't need all 375 digits of precision, then what may be better is to calculate the logarithm (or log10() ) of the factorial. Of coarse this doesn't give you exactly what you want, but it is something you don't need a custom library for.
- Kevin
For arbitrary long integers try to Google for NTL.
if you ask me, recursion is the way to go on this.
i just wrote this on the spot and it works but you might want to add some more error checking (dont let your user give you a negative value or else uh oh!)
int Factorial(int a)
{
if(a == 1) return 1;
else
{
return (a * Factorial(a-1));
}
}
It is well-known that the recursive approach to calculating a factorial is orders of magnitude slower than an iterative approach. Also, do you really think the call stack is going to stand up to potentially hundreds of calls sitting on it?
I feel cold not cool, my sister :);)Quote:
Originally posted by galathaea
There are many arbitrary precision integer arithmetic class libraries available. I have used them alot in numerics, and yeas, they are very cool! ;)
Anyway i think Micheal Williamson's method is pretty good...
you're entirely right. sorry for trying to help or adding some input.
i didn't say DONT USE ANY OTHER METHOD, USE THIS! i simply offered another possibility.