0

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.

Edit Regarding Didier Trosset's question , i thought that surely 20 wont be the limit upto which people can calculate a factorial in C. I was reading that uintmax_t is the biggest data type , so i thought it may be 128bits or something. So i wanted to use it.

Also , I didn't know about big int libraries in C. Sorry if i created a duplicate post.

Somjit
  • 2,503
  • 5
  • 33
  • 60
  • 4
    `uintmax_t` isn't going to help - you need to look at using a suitable bit integer library. – Paul R Sep 06 '13 at 08:26
  • 1
    May be [GMP](http://gmplib.org/) ? – P0W Sep 06 '13 at 08:26
  • Many duplicates, e.g. [What is the simplest way of implementing bigint in C?](http://stackoverflow.com/questions/3340511/what-is-the-simplest-way-of-implementing-bigint-in-c) and ["BigInt" in C?](http://stackoverflow.com/questions/565150/bigint-in-c) – Paul R Sep 06 '13 at 08:26

0 Answers0