91
\$\begingroup\$

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.

\$\endgroup\$
12
  • 15
    \$\begingroup\$ 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)? \$\endgroup\$
    – Ami
    Feb 6, 2011 at 22:43
  • 7
    \$\begingroup\$ @SHiNKiROU, even golfscript can manage 125! less than 1/10th of a second and it's and interpreted interpreted language! \$\endgroup\$
    – gnibbler
    Feb 8, 2011 at 3:21
  • 8
    \$\begingroup\$ Completes in under a minute seems a very hardware-dependent requirement. Completes in under a minute on what hardware? \$\endgroup\$
    – sergiol
    Aug 24, 2017 at 18:05
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ Aug 24, 2017 at 21:20
  • 5
    \$\begingroup\$ 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. \$\endgroup\$
    – MilkyWay90
    May 7, 2019 at 2:02

216 Answers 216

1
4 5 6 7
8
0
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ 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/… \$\endgroup\$
    – user25406
    Mar 1, 2023 at 16:55
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – The Thonnu
    Mar 1, 2023 at 18:04
  • \$\begingroup\$ I found some Python answers but not JS. Can you please provide a link to it. Thanks. \$\endgroup\$
    – user25406
    Mar 1, 2023 at 19:35
  • 1
    \$\begingroup\$ codegolf.stackexchange.com/… \$\endgroup\$
    – The Thonnu
    Mar 1, 2023 at 19:37
0
\$\begingroup\$

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)

\$\endgroup\$
0
\$\begingroup\$

ARBLE, 16 bytes

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

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Yabasic, 60 bytes

Port of VBA answer.

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Google Sheets, 26 Bytes

Anonymous worksheet function that takes input, \$n\$, from cell [A1] and outputs \$n!\$ to the calling cell.

=Product(Sequence(Max(A1,1

Excel Version, 29 Bytes

Listed separately as Excel throws an error for unpaired parens.

=PRODUCT(SEQUENCE(MAX(A1,1)))
\$\endgroup\$
1
4 5 6 7
8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.