81
\$\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\$
13
  • 13
    \$\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 '11 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 '11 at 3:21
  • 6
    \$\begingroup\$ @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. \$\endgroup\$ Jan 7 '14 at 3:18
  • 5
    \$\begingroup\$ Completes in under a minute seems a very hardware-dependent requirement. Completes in under a minute on what hardware? \$\endgroup\$
    – sergiol
    Aug 24 '17 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 '17 at 21:20

191 Answers 191

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

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

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

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

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)

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

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

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

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).

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

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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;} \$\endgroup\$
    – mgthomas99
    Sep 11 '18 at 21:45
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ Sep 12 '18 at 7:48
0
\$\begingroup\$

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

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!

\$\endgroup\$
1
  • \$\begingroup\$ @JoKing changed the answer to reflect 🤓 \$\endgroup\$ Feb 19 '19 at 12:05
0
\$\begingroup\$

Kotlin, 50 45 bytes

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

Thanks to @jonathan-frech

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

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

\$\endgroup\$
2
  • \$\begingroup\$ Try it online! -3 chars by putting f= in the header part and removing the space between 0 and else \$\endgroup\$ Jun 16 at 4:27
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 16 at 4:29
1
3 4 5 6
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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