# 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! – gnibbler 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. – Michael Stern 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? – sergiol 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. – Kevin Brown Aug 24 '17 at 21:20

# Pepe, 60 bytes

rrEERREeeeeeeeErEeREeEreEREErEEEEErEEEeeReererRrEEEEEEeEreEE


Try it online!

# C, 37 bytes

long double f(n){return!n?1:n*f(n-1);}


This program accepts one number from stdin and then prints the answer in floating point form.

• Hello, and welcome to Code Golf! This is a great answer, but to reduce the byte count even more, why not remove the extra spaces? – NoOneIsHere Aug 3 '19 at 22:23
• Oh, so you only want the nonmain function. Alright. I will keep that in mind for next time. – T. Salim Aug 3 '19 at 22:33
• You are right Jo, I fixed it. – T. Salim Aug 4 '19 at 0:05
• Thank you A__, I removed the parentheses. – T. Salim Aug 4 '19 at 2:41
• No, I tried to simply use long but the computer outputted 0 due to overflow, so I kept it as long double. – T. Salim Aug 4 '19 at 2:45

# Pip, 13 bytes

Fi,a{o*:i+1}o


Try it online!

Recursive factorial is given in Pip documentation, so I did iterative. Also, it couldn't handle up to 125! anyway.

# Keg, 95 4 bytes

Ï⑨∑*


Try it online!

-1 byte thanks to @A̲̲

### 5 Bytes

Ï_1∑*


Try it online!

This:

• Takes the (Ï)ota of the implicit input (pushes input, input - 1, input - 2 ... 0)
• Pops the bottom 0
• Pushes an extra 1 (so that the 0 case works)
• Multiplies the entire stack together