how to calculate factorial of number in C

Jun 21, 2012 at 7:51pm

I just want to calculate the factorial of a number 450000 in C by using "larger integer airthmatic" concept of data structure in C

or any number which has higher numeric value than the given range of integer
Jun 21, 2012 at 8:58pm
Once a time I had the same problem, i.g. I had to work with big-integer, so this time I wrote a class called BigInteger which solved my problem.

Search the web for Big-Inetger, you may find some classes written by some smart people, if you don not find any, than write your own.

Jun 22, 2012 at 6:54pm
@therockon7throw
thanx

write your class or deomnstrate me how to write my own class on Big Integer
because I have never written a class in C except structure,union & enum
Jun 22, 2012 at 11:25pm
This is homework, right?
Jun 22, 2012 at 11:47pm
easy off the top of my head. realize that factorial is a summation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

float factorial( float n )
{
      float x = n;
      float s = n - 1;

      for( ; s > 0 && x > 0; x -- )
     {
         s *= x;
      }

     return s != 0 ? s : 1;//!0 = 1 (special case)
}


probrably shouldn't have coded it for this forum if it was homework.

edit: correct me if I'm wrong, but factorial is only valid for whole numbers.
double edit: the factorial of such a large number would take forever and would be impossible on some computers for such a large number.

!450000 (are you insane, its over 9000!) wtf.
Last edited on Jun 23, 2012 at 12:43am
Jun 23, 2012 at 2:14am
Not insane, it's an assignment with a sufficiently large factorial to ensure that the bignum class used is flexible enough to cope. Remember factorials get big really quickly. And DeXecipher, please don't post solutions to homework.
Jun 23, 2012 at 4:05am
For the record, 450000! takes up less than 960 KiB of memory (not counting the space to calculate it). Far from impossible to compute, even on the smallest of modern computers. In fact, a while back there was a thread where we tried various methods to compute even larger factorials, up to one million.
Jun 23, 2012 at 10:15am
Nothing is impossible :-) If a C++ library is able to compute the determinat, inverse of a matrix ( and apply linear algebra on matrices) being large than 1000000X1000000 dimension.. it is possible, you need to try and try and try till you find a way...

@ JAI SINGH
Sorry I cant give you the class, you need to try it yourself, learn by doing by trying, not by coping... once you have your solution than we may provide you other options/solutions
Jun 23, 2012 at 4:45pm
what I meant is that the average sized data type cannot store it, which goes back to the Big Integer problem. Its too big for your average (int) and would cause an overflow error.
Topic archived. No new replies allowed.