factorials

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsn
    New Member
    • Sep 2007
    • 237

    factorials

    hello everyone in my first programming subject our Dr. told us to find the factorial of n where n<10.
    after that i tired to get the factorial of bigger number like 50 or 100
    it doesn't work it give worng anser all the time. you know because the output will be a huge number (VERY HUGE).
    what should i do to find the factorial of 100.
    is there any special technique i should use or what.

    regards
    hsn
  • Simonius
    New Member
    • Feb 2008
    • 47

    #2
    If I remember it correctly isn't the factorial of 100 written as 100!
    And isn't that basicly 1*2*3*...*98*99 *100?

    So basicly you want to do it while your counter is smaller or equals n.
    And your counter starts at 1 and every loop it needs to be incremented by one.

    If you can't find the solution by coding then try writing down what you want to achieve ;)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      The problem, that you seem to be aware of is that a 32 bit integer is not big enough to hold the result, in fact a 32 bit integer can hold up to 12! but not 13!.

      There are a number of possible solutions a bigger integer is the most obvious thought, many systems have 64 bit integer today. Unfortunately that will not help, a 64 bit integer can hold up to 20!.

      You could also use doubles to get an approximation to the factorial, but the result will not be accurate.

      What you really need is a much much larger integer. The way to do this in C++ is to create a class that acts as a much larger integer using multiple integers to hold digits in your large integer.

      You could write this from scratch, but you can also find implementations on the web already, I know GNU has a large integer class.

      Comment

      Working...