Thread: Expanding Factorial Limits

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    10

    Expanding Factorial Limits

    Code:
    #include <stdio.h>
    #include <conio.h>
             int main(){
                 int ts;
                 int carpim;
                 printf("Enter Integer : ");
                 scanf("%d",&ts);
                 carpim=1;
                 while(1<ts){
                              carpim=carpim*ts;
                              ts=ts-1;
                              }
                              printf("Factorial = %d 'dir",carpim);
                              getch();
                              return 0;
                              }
    in the code you see everything is clear. but when i try to calculate 18 factorial is give output as " -898433024 " . what must i do help me

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    One thing you can do is to use an unsigned. However that won't get rid of the problem. you could use a double and that could store more numbers accuractly. But the real problem comes in that 18! is really big. And if you kept going higher and higher you'll eventually get to a point that a double can't store it accuratly. So you'll have to start looking into a big int library

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You could also consider finding out the memory limit of your doubles and if your factorial calculation is reaching the highest value, it can output that number and do the rest in another calculation and output that.

    Think of a calculator that gets overloaded on digits. It will output as many digits as it can then give an e to a certain power to express the rest. You could do something similar. It might take a bit of thinking to make it work, but it's just a suggestion.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    10
    it seems my problem is big to solve. if someone can eidt the code and make i will be happy.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    When calculating very large factorials you might want to use Stirling's approximation (which is much faster), http://en.wikipedia.org/wiki/Stirling%27s_approximation . To do 5! you would do:
    n = sqrt((2 * M_PI) * 5) * pow((5 / 2.718281828459), 5); This is useful because big int libraries are normally a fair bit slower than normal ints and so that is normally a better method. It is however, only an approximation of the factorial.
    Last edited by EvilGuru; 11-06-2005 at 06:38 AM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by SNaRe
    it seems my problem is big to solve. if someone can eidt the code and make i will be happy.
    That's a very bold statement to make. Considering you have no idea how difficult this actually could be, you're asking some stranger to do your work for free because you can't handle it?
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  3. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 07:39 AM
  4. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM