Love_U_JERRY's blog

By Love_U_JERRY, 11 years ago, In English

How to calculate factorial of larger integer numbers ( i.e. numbers greater than 20 ) using C/C++ using high precision arithmetic ?

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
11 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Implement big numbers. Like BigInteger in Java.

»
11 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Is there any algorithm can do this in O(logN) or less than O(N) ?

  • »
    »
    11 years ago, # ^ |
    Rev. 2   Vote: I like it +9 Vote: I do not like it

    Factorial grows exponentially. So you need at least O(n) time and memory to calculate it precisely.

    • »
      »
      »
      11 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Also a^N grows exponentially but we can calculate it in O(logN)

      • »
        »
        »
        »
        11 years ago, # ^ |
          Vote: I like it +9 Vote: I do not like it

        You can calculate it fast unprecisely: either modulo some number or in floating-point type. Anyway, I haven't heard about any fancy factorial calculation algorithms that use less than O(n) arithmetic operations.