Questions tagged [factorial]

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

A factorial of a non-negative integer n, is the product of all positive integers up to n, and is denoted with an exclamation mark, n!.

  • 5! = 5*4*3*2*1 = 120
  • 20! = 1*2*3*...*18*19*20 = 2 432 902 008 176 640 000 (largest factorial representable as a 64-bit )
  • 69! ≈ 1.72 × 1098, 70! ≈ 1.20 × 10100
  • 0! = 1 (by definition)

Factorial implementation

In computer programming, the function which computes the factorial of a number is often used as an example of a recursive function. Here is a simply recursive Python code for the factorial function.

# Calculates factorial of a number n! recursively
def factorial(n):
    # Base case, stop when n is 0 or 1
    if n == 0 or n == 1:
        return 1
    # Recursive step
    return n*factorial(n-1)

Here is an equivalent iterative version of the factorial function:

# Calculates factorial of a number n!
def factorial(n):
    # Base case, stop when n is 0 or 1
    if n == 0 or n == 1:
        return 1
    # Loop to multiply by successive integers
    product = 1
    counter = 2
    while counter <= n:
        product *= counter
        counter += 1
    return product

Applications of the factorial

The factorial is important in fields of combinatorics. The most well-known example of factorial is to count the ways n objects can be arranged in a line - the number of ways (permutations) is n!. The binomial coefficient nCk is equivalent to n!/k!(n-k)!. It also serves a role in evaluating power series of well-known functions such as .

Factorial time and complexity is also one of the commonly referred complexity levels of an algorithm, namely O(n!) complexity. The factorial function grows very fast, and grows asymptotically faster than any exponential function an with linear exponent, so therefore factorial time and complexity indicates very poor asymptotic performance. In fact, a 64-bit long integer can only store up to 20!, and 69! is the largest factorial less than a googol.

Enumerating and generating a list of all the permutations of n objects takes factorial time and space, and quickly becomes infeasible for even small integers such as 10.

See more

1356 questions
114
votes
49 answers

What is the fastest factorial function in JavaScript?

Looking for a really fast implementation of the factorial function in JavaScript. Any suggestions?
Ken
  • 1,605
  • 3
  • 13
  • 20
101
votes
4 answers

Which function grows faster, exponential or factorial?

Which function grows faster, exponential (like 2^n, n^n, e^n etc) or factorial (n!)? Ps: I just read somewhere, n! grows faster than 2^n.
devsathish
  • 2,339
  • 2
  • 20
  • 16
100
votes
20 answers

Ruby factorial function

I'm going crazy: Where is the Ruby function for factorial? No, I don't need tutorial implementations, I just want the function from the library. It's not in Math! I'm starting to doubt, is it a standard library function?
rutger
  • 2,315
  • 4
  • 16
  • 9
92
votes
16 answers

Example of O(n!)?

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I'm asking about time complexity.
Derek Long
  • 1,169
  • 1
  • 11
  • 15
63
votes
4 answers

Example of a factorial time algorithm O( n! )

I'm studying time complexity in school and our main focus seems to be on polynomial time O(n^c) algorithms and quasi-linear time O(nlog(n)) algorithms with the occasional exponential time O(c^n) algorithm as an example of run-time perspective.…
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
47
votes
8 answers

Fast way to calculate n! mod m where m is prime?

I was curious if there was a good way to do this. My current code is something like: def factorialMod(n, modulus): ans=1 for i in range(1,n+1): ans = ans * i % modulus return ans % modulus But it seems quite slow! I also…
John Smith
  • 11,678
  • 17
  • 46
  • 51
44
votes
5 answers

OverflowError: long int too large to convert to float in python

I tried to calculate poisson distribution in python as below: p = math.pow(3,idx) depart = math.exp(-3) * p depart = depart / math.factorial(idx) idx ranges from 0 But I got OverflowError: long int too large to convert to float I tried to convert…
user2312186
  • 453
  • 1
  • 4
  • 8
44
votes
13 answers

Best way of calculating n choose k?

What is the most efficient method to evaluate the value of "n choose k" ? The brute force way I think would be to find n! / k! / (n-k)! by calculating each factorial separately. A better strategy may be to use DP according to this recursive formula,…
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
42
votes
1 answer

Variables Multiplication for the factorial calculation

I'm making a script that calculates the factorial for a given number, but I'm having some problems with the multiplication. Note: the factorial for is given by: 9!=9*8*7*6*5*4*3*2*1 Here's my code: #!/bin/bash echo "Insert an Integer" read…
UraniumSnake
  • 465
  • 1
  • 4
  • 7
39
votes
8 answers

Built-in factorial function in Haskell

I know this sounds like a stupid question, but here it is: Is there a built-in factorial in Haskell? Google gives me tutorials about Haskell explaining how I can implement it myself, and I could not find anything on Hoogle. I don't want to rewrite…
Simon Bergot
  • 10,378
  • 7
  • 39
  • 55
38
votes
1 answer

Why is math.factorial much slower in Python 2.x than 3.x?

I get the following results on my machine: Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import timeit >>>…
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
37
votes
8 answers

Compare two factorials without calculating

Is there any way to compare which factorial number is greater among two numbers without calculating? The scenario is i am creating a c# console application which takes two factorial inputs like 123!!!!!! 456!!! all i want to do is to compare…
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
34
votes
4 answers

Complexity of recursive factorial program

What's the complexity of a recursive program to find factorial of a number n? My hunch is that it might be O(n).
Karan
  • 11,509
  • 8
  • 34
  • 38
33
votes
19 answers

How do I find a factorial?

How can I write a program to find the factorial of any natural number?
Kraken
  • 23,393
  • 37
  • 102
  • 162
30
votes
4 answers

Fast algorithms for computing the factorial

I found FastFactorialFunctions describing a number of algorithms for computing the factorial. Unfortunately, the explanations are terse and I don't feel like sifting through line after line of source code to understand the basic principles behind…
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
1
2 3
90 91