Friday, 6 September 2013

Factorial calculation : unsigned long long overflows, How to use uintmax_t?

Factorial calculation : unsigned long long overflows, How to use uintmax_t?

calculating a factorial in this method :
unsigned long long factorial(int n){
unsigned long long temp = 1L;
for(int i = 0 ; i < n ; i++){
temp *=(n - i);
}
printf("\n%d factorial is %llu",n,temp);
return temp;
}
Allows calculation upto 20! only.
20! = 2432902008176640000 << 2^64
21! = 51090942171709440000 >> 2^64
I'v never used uintmax_t and reading from a few other SO articles , it all
feels really complicated to use. If anyone could give some simple snippets
as how to use and print them , it would be a major help.

No comments:

Post a Comment