# 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, 2011 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, 2011 at 3:21
• Completes in under a minute seems a very hardware-dependent requirement. Completes in under a minute on what hardware? Aug 24, 2017 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, 2017 at 21:20
• Why aren't built-ins allowed? You haven't specified what built-ins are, and if you said that it was up to a "reasonable person" to decide (which is completely subjective, but ignoring that), you still say that any form of eval is a built-in for the factorial, even though it evaluates code, not the factorial of a given number. May 7, 2019 at 2:02

# Thunno, $$\ 4 \log_{256}(96) \approx \$$ 3.29 bytes

R1+P


Attempt This Online!

But, if built-ins were allowed:

F


Attempt This Online!

#### Explanation

R1+P  # Implicit input
R     # range(0, input)
1+   # increment
P  # product
# (for an empty list
# this gives 1)
# Implicit output

• How would the method posted on sci.comp.stackexchange.com performs compared to the above methods. The method basically cut the number of multiplications by half. See link below. I cannot code but I am curious to know how the method performs. scicomp.stackexchange.com/questions/42510/… Mar 1, 2023 at 16:55
• I'm not too sure but since Thunno is a golfing language it might not be the best for comparison. You might want to compare a Python or JS answer instead. Mar 1, 2023 at 18:04
• I found some Python answers but not JS. Can you please provide a link to it. Thanks. Mar 1, 2023 at 19:35
• codegolf.stackexchange.com/… Mar 1, 2023 at 19:37

# Itr, 4 bytes

1#R*

online interpreter

## Explanation

1     ; push 1
#    ; read a number from standard input
R*  ; reduce the range from 1 to that number with * as operator
; implicitly print the result of the reduction or the 1 if the range was empty


# Itr, 2 bytes:

#P

Product over the range from 1 to the input. (could be considered a factorial built-in)

# ARBLE, 16 bytes

range(1,n)/(x*y)


Reduce the range of 1-n by x*y.

Try it online!

# VBA, 55 Bytes

Subroutine that takes input $$\n\$$ and outputs $$\n!\$$

Function f(n)
If n Then f=n*f(n-1)Else f=1
End Function


# Yabasic, 60 bytes

Sub f(n)
If n Then Return n*f(n-1)Else Return 1EndIf
End Sub


Try it online!

Anonymous worksheet function that takes input, $$\n\$$, from cell [A1] and outputs $$\n!\$$ to the calling cell.
=Product(Sequence(Max(A1,1

=PRODUCT(SEQUENCE(MAX(A1,1)))