# Find the Factorial!

Create the shortest program or function that finds the factorial of a non-negative integer.

The factorial, represented with ! is defined as such

$$n!:=\begin{cases}1 & n=0\\n\cdot(n-1)!&n>0\end{cases}$$

In plain English the factorial of 0 is 1 and the factorial of n, where n is larger than 0 is n times the factorial of one less than n.

Your code should perform input and output using a standard methods.

Requirements:

• Does not use any built-in libraries that can calculate the factorial (this includes any form of eval)
• Can calculate factorials for numbers up to 125
• Can calculate the factorial for the number 0 (equal to 1)
• Completes in under a minute for numbers up to 125

The shortest submission wins, in the case of a tie the answer with the most votes at the time wins.

• How many of the given answers can actually compute up to 125! without integer overflow? Wasn't that one of the requirements? Are results as exponential approximations acceptable (ie 125 ! = 1.88267718 × 10^209)?
– Ami
Feb 6 '11 at 22:43
• @SHiNKiROU, even golfscript can manage 125! less than 1/10th of a second and it's and interpreted interpreted language! Feb 8 '11 at 3:21
• @ugoren the two-character solution to the other question uses a built-in factorial function. That's not allowed in this version of the challenge. Jan 7 '14 at 3:18
• Completes in under a minute seems a very hardware-dependent requirement. Completes in under a minute on what hardware? Aug 24 '17 at 18:05
• @sergiol Incredibly that hasn't been an issue in the last 2 years, I suspect most languages can get it done in under a minute. Aug 24 '17 at 21:20

# Javascript (No body version), 23 bytes

f=(n,t=n?n*f(n-1):1)=>t

Saved 2 bytes with n?n, got the idea from Drew Christensen. This is what I had before

f=(n,t=0**n||n*f(n-1))=>t

# 4, 37 bytes

3.70060101602018002010100100000295014


Try it online!

If you question the input method, please visit first the numerical input and output may be given as a character code meta post.

### Pseudo code

g_0 = input()
g_1 = 1
while g_0 != 0:
g_1 *= g_0
g_0 -= 1
print(g_1)


# Forth (gforth), 31 bytes

: f 1e 0 ?do i 1+ s>f f* loop ;


Try it online!

Answer will be on the floating point stack as 125! exceeds the maximum double precision integer value (by a lot)

# Stax, 4 bytes

┤c5ô


Run and debug it online

Unpacked version with 5 bytes:

!x+k*


## Explanation

!x+      Compute logical not, then add to itself
The result is denoted m
k*    reduce with multiplication over the range [1..m]


# Gol><>, 8 bytes

1IFLP*|h


Try it online!

### How it works

1IFLP*|h

1         Push 1
I        Take input (n) as int
F   |   Repeat n times...
LP*      Multiply loop counter(L) + 1; L = 0..n-1
h  Print the top as int and halt


## Function form, 8 bytes

Assuming that stack input and stack output are allowed for Gol><> functions.

1$FLP*|B  Try it online! Takes the stack as input (assuming the stack has only one value), and leaves the factorial as the only content of the stack. $ is used to push 1 to the bottom, and B is the "return" command. Note that B inside an F (for) or W (while) loop acts as a "break" instead.

### Using the Gol><> function

The following code uses the function above to output 0! to 125!.

1AG~FLGN|;
1$FLP*|B 1AG Register row 1 as function G ~ 126 F | Repeat from 0 to 125... Stack is empty at this point L Push loop counter G Call G on the current stack N Pop and print as number, with newline ; Halt  # dc, 22 bytes [1q]sg[d2>gd1-d2<f*]sf  Try it online! (requires Javascript) This is the obvious 12-byte implementation ([d1-d1<f*]sf) with an additional test to short-circuit the calculation for numbers less than 2 (which otherwise subtract too far, yielding a product of zero). # Java (JDK 10), 84 bytes n->{var r=java.math.BigInteger.ONE;for(;n>0;)r=r.multiply(r.valueOf(n--));return r;}  Try it online! ## Credits • You can save 5 bytes by using Java 9's var and removing the r=null initialisation: n->{var r=java.math.BigInteger.ONE;for(;n>0;)r=r.multiply(r.valueOf(n--));return r;} Sep 11 '18 at 21:45 • @mgthomas99 Indeed! But it's Java 10's var, not Java 9's ;) and it wasn't released at the time I wrote that answer. But I have no issue switching to Java 10. Sep 12 '18 at 7:48 # Japt, 4 bytes oÄ ×  Try it ## Explanation o :Range [0,input) (= empty array when input is 0) Ä :Add 1 to each × :Reduce by multiplication, with an initial value of 1  # PHP, 40 39 bytes <?=array_product(range($argv[1]?:1,1));


Thanks to @JoKing for saving 1 byte and correcting the input

Try it online!

• @JoKing changed the answer to reflect 🤓 Feb 19 '19 at 12:05

# Kotlin, 50 45 bytes

fun f(x:Int):Int=when(x){0->1;else->x*f(x-1)}


Thanks to @jonathan-frech

• Welcome to PPCG. I think the 1->1; clause is superfluous. Feb 19 '19 at 14:22

# Python 3, 32 29 bytes

lambda n:n*f(n-1)if n>0else 1


Try it online! Just a simple recursive solution I made for Generalised multi-dimensional chess knight's moves.

Thanks for the -3 Maanas

• Try it online! -3 chars by putting f= in the header part and removing the space between 0 and else Jun 16 at 4:27
• oh, I didn't realise that was something you could do with the f= bit as I originally just wrote the function in sublime text. Jun 16 at 4:29