Large Factorials in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saiavinashiitr
    New Member
    • Mar 2014
    • 16

    Large Factorials in C++

    Can anyone tell me how to get large fatorials in c++...in c++ i am getting the ouput upto maximum of 20!
    any please help how to solve this issue.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The problem is that factorials quickly get too large to be represented in the standard C integer types.
    • unsigned 8-bits can hold at most 5!
    • unsigned 16-bits can hold at most 8!
    • unsigned 32-bits can hold at most 12!
    • unsigned 64-bits can hold at most 20!
    • Excel formula for bits required to hold n!: ROUNDUP(LOG(FAC T(n),2),0)

    Comment

    • saiavinashiitr
      New Member
      • Mar 2014
      • 16

      #3
      @dinbock:
      i haven't understood the last statement "ROUNDUP(LOG(FA CT(n),2),0)"
      could u please explain it briefly

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You can express a large number as a string or as an array.

        In C++ you would design a class to contain the array with member functions that work with the array to let you do math on the elements.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          @saiavinashiitr -that last line is an Excel formula. For example, to find out how many bits it takes to hold the value of 100!, type this into an Excel spreadsheet cell: =ROUNDUP(LOG(FA CT(100),2),0)
          • FACT(100) returns the value of 100!, call it a.
          • LOG(a,2) returns the base 2 logarithm of 100!., call it b.
          • ROUNDUP(b,0) rounds the logarithm up to the next integer.

          Comment

          Working...