# 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

# PHP

## Short, 58

<?$r=$i=$argv[1];while($i>1){$i--;$r=$r*$i;}echo($r==0?1:$r);


### Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

### Ungolfed

<?php
$r =$i = $argv[1]; // Set$r and $i to Arg. while($i > 1) // Calculate while $i bigger than 1 {$i--; // Decrement $i (so it's not infinite)$r = $r *$i; // Calculation the Factorial
}
echo ($r==0 ? 1:$r); // Output and make 0! = 1
?>


## Slighty Longer, 86

<?$r=$i=(isset($argv[1])?$argv[1]:0);while($i>1){$i--;$r=$r*$i;}echo($r==0?1:$r)."\n";  ### Improvements • Output with \n • Doesn't throw error if no arg defined # DUP, 19 bytes [$[$1-a;!*][%1]?]a:  Try it here! A recursive lambda that leaves result on the stack. Usage: 6[$[$1-a;!*][%1]?]a:a;!  # Explanation [ ]a: {set a to lambda}$                  {check if top of stack >0}
[       ][  ]?    {conditional}
$1-a;!* {if so, top of stack *a(top of stack -1)} %1 {otherwise, replace top of stack with 1}  ## Hoon, 29 bytes |=(@ (reel (gulf [1 +<]) mul)  Hoon's native number is a bignum, so it works fine with 125 (or even 2000). It also correctly gives 1 for 0. It uses +< in order to access the sample of the gate. This is axis navigation syntax: It means to access the tail of the subject, and then the head, which is where the sample is stored in the binary tree model Hoon uses. Urbit drops you into a shell and Hoon REPL when you start it, :dojo. To test this, simply enter %. 125 on one line and then the snippet for 125! Note there are two spaces between the dot and 1. • Hoon is a beautiful mystery. – Lynn Mar 1 '16 at 1:10 • It's a surprisingly nice language to code in! It takes a little bit to learn all the runes, but you don't even need to internalize them to read it since they belong in "families" based on the first symbol. The fact it's strongly typed and has a novel type system is just icing on the cake. – RenderSettings Mar 1 '16 at 1:36 # Racket, 29 (λ(x)(apply * x(range 1 x)))  • Can you explain how this solution works? Not everyone is familiar with Racket. – overactor Sep 9 '14 at 10:15 • @overactor It creates an anonymous function, creates a list from 1 to the given number, and applies multiplication on all of it. – Mega Man Aug 5 '19 at 18:23 • – EdgyNerd Nov 26 '19 at 15:57 # Desmos, 18 bytes a=1 \prod _{n=1}^an  Uses the formula for a! instead of a! # Joy, 13 bytes [1][*]primrec  30 char requirement in codegolf? • @FryAmTheEggman To make a function you would actually have to write DEFINE f==[1][*]primerec.. I believe program arguments are thrown on the stack when the program starts, and that everything outside a define block is executed – BlackCap Jun 8 '16 at 20:12 • It'd be best if you knew for sure rather than just believing ;) Anyway, seems alright then, but you also wouldn't run in to that 30 character requirement if you gave some explanation of your code :P – FryAmTheEggman Jun 8 '16 at 20:16 # Yup, 33 31 29 bytes *{{:0e-}]~{~|~|0~--e~}~#\}0e#  Here's the github. Invoke like this: node yup.js <location>.yup -n <input>  Or node yup.js -l "*{{:0e-}]~{~|~|0~--e~}~#\}0e#" -n <input>  Examples: λ node yup.js -l "*{{:0e-}]~{~|~|0~--e~}~#\}0e#" -n 5 120 λ node yup.js examples\factorial.yup -n 0 1  ## Explanation *{{:0e-}]~{~|~|0~--e~}~#\}0e# *  take input { }  while TOS -- if zero, we advance to the } 0e#  print number 1 (exp(0))  otherwise (nonzero) { }  while TOS is not zero :  duplicate TOS 0  push 0 e  pop 0, push exp(0) = 1 -  subtract 1  we eventually are at zero. ]  we move that zero to the bottom of the stack ~  switch top two for looping offset {~ ~}  while STOS |~|0~--e  multiply two elements (see further down) ~  switch the top zero with the result #  print the result \  exit program (so we don't print the final one)  ### Multiplication In this program, I have multiplication defined as thus: |~|0~--e  First, observe 0~--. This pushes a zero behind the TOS, and subtracts twice: command | stack | a b 0 | a b 0 ~ | a 0 b - | a (-b) - | a - (-b) = a + b  This performs addition. Let's replace 0~-- with + for clarity: |~|+e  Now, | is ln. So watch the stack: command | stack | a b | | a ln(b) ~ | ln(b) a | | ln(b) ln(a) + | (ln(b)+ln(a)) e | exp(ln(b)+ln(a))  And, by the theorem of logarithms, this is multiplication. # Maple, 17 bytes n->*(seq(1..n))  Usage: > f:=n->*(seq(1..n)); > f(0); 1 > f(5); 120 > f(125); 188267717688892609974376770249160085759540364871492425887598231508353156331613598866882932889495923133646405445930057740630161919341380597818883457558547055524326375565007131770880000000000000000000000000000000  # Fourier, 18 bytes Non-competing: Fourier is newer than the challenge 1~NI(i^~iN*i~Ni)No  Try it online! ## Oasis, 3 bytes Try it online n*1  Explanation: n*1 n Push n * Multiply the two items on the top of the stack Because there is only one item on the stack, A(n - 1) is pushed Implicit output 1 Special case A(0) = 1  # Pushy, 3 bytes Non-competing as the language postdates the challenge. RP#  Explanation: R \ Push the inclusive range of the input P \ Push the product # \ Print  Try it online! • Factorial built-ins aren't allowed, sorry. – ETHproductions Jan 19 '17 at 20:16 • Is that in the question? I must have missed it. – user63571 Jan 19 '17 at 20:20 • You can do RP# which gets the range and prints the product. – FlipTack Jan 19 '17 at 20:23 • Thanks for using Pushy, but as it postdates this challenge you should mark the answer as non-competing (and maybe include a TIO link) – FlipTack Jan 19 '17 at 20:24 • I'm a bit hazy on the time Pushy was made – user63571 Jan 19 '17 at 20:31 # Python, 25 bytes f=lambda x:x<2or x*f(x-1)  Try it online! This is a recursive lambda. It returns True if the factorial is 1 (inputs 1 and 0), but that's allowed by meta. ## Alice, 13 bytes /o \i@/r.1~&*  Try it online! ### Explanation This is a basic framework for arithmetic programs to read and write integer I/O and process them in Cardinal mode: /o \i@/...  As for the actual computation: r Range: Replace the input N with 0, 1, 2, ..., N. . Duplicate N. 1~ Put a 1 underneath the copy to initialise the product correctly for N = 0. & Repeat the next command N times. * Multiply (N times, multiplying up the entire stack).  ## Powershell, 38 bytes filter f{((1..$_-join'*'|iex),1)[!$_]}  I used some of the other answers here for inspiration, but I'm not lower than @Joey. Although, I'm not sure how their code knows to stop subtracting once it hits 0... PS C:\> 0|f 1 PS C:\> 4|f 24 PS C:\> 125|f 1.88267717688893E+209  Pyth, 15 K1FNr1hQ=K*KN;K Try it here! If you only need one variable, it’s better to use K or J which don’t need an equals sign to be assigned to on their first use Pyth, 7 Bytes u*GhHQ1  Try it online! This is pretty much simple. Pyth is a good choise for code golfing. # TI-BASIC, 65 14 bytes For(I,0,Ans:IAns+not(Ans:End  Tricky tricky... +not(Ans and loop starting from zero should handle the special case. seq( isn't as viable of an option for that reason. TI-Basic only supports up to 10^100 which makes 70! and above fail, but it's easy to see that this solution would extend indefinitely. • What's wrong with For(? – Khuldraeseth na'Barya Sep 9 '17 at 2:01 • @lirtosiast I apologize for the poor quality of the previous edit, which I fixed. – Timtech Sep 9 '17 at 13:33 • @Scrooble Nothing, I just didn't understand TI-Basic as well in Apr '14. – Timtech Sep 9 '17 at 13:34 # Pyth, 3 bytes *FS  Try it here. • the S appears to no longer be necessary – hakr14 Sep 12 '18 at 18:20 # K (oK), 5 bytes Solution: */1+!  Try it online! Explanation: Interpretted right-to-left: */1+! / solution ! / til, performs range, 0..n 1+ / adds 1 (vectorised) */ / multiply-over elements in list  Notes: Unlike the K/Kona/Q implementations, the input is treated as a float: oK)@5 -9 q)type 5 -7h  # ><>, 17 16 bytes 1v;n$<*}-1.!?::


-1 byte thanks to Jo King.

Since the question asks for a function as opposed to a full program, I allowed myself to accept the input from the stack without counting an additional 3 bytes for using the -v option.

This manages to be shorter than the other ><> answer because it jumps to the end-of-iteration code without having to hardcode the jump destination address : the current iteration counter (duplicated) is used as an address.
The iteration stops when the counter is 0, and jumping to (0, 0) while the direction pointer points to the right will execute the n; code that is otherwise unreachable, displaying the result and stopping the execution.

It handles 0! correctly and executes in 10*(n+1) ticks for n > 0 or 9 ticks for n = 0.

You can try it online.

• If you run the bottom line to the left so that the pointer is moving left when it is transported, You remove the need for the second line. -1 byte 1v;n \n $<*}-1.!?:: – Jo King Dec 20 '17 at 5:41 • Don’t forget you can remove the ; as well! – Jo King Dec 21 '17 at 12:41 # Pyt, 1 2 bytes řΠ  Try it online! Implicit input ř creates a range from 1 to input, taking care of the 0 edge case Π multiplies everything together Implicit output  • Builtin is not allowed. – Weijun Zhou Feb 22 '18 at 3:16 • @WeijunZhou Ah yes, I will update, thank you – FantaC Feb 22 '18 at 14:55 • @Weijun Zhou and downvoters, please retract, I have taken out the builtin – FantaC Feb 22 '18 at 15:01 Python - 74 Python without any library calls. Not the shortest but calculate factorial of 125 in a flash (< 1 sec) def factorial(n): if n < 1: return 1 else: return n * factorial(n-1)  Output of 125 factorial 188267717688892609974376770249160085759540364871492425887598231508353156331613598866882932889495923133646405445930057740630161919341380597818883457558547055524326375565007131770880000000000000000000000000000000  • For some reason my indentation is not getting preserved. – Ricardo A Jun 6 '14 at 3:59 • Someone indented it for you, and then I reduced the indentation to only a single space per indent. I also added a character count. – Rainbolt Jun 6 '14 at 21:45 • You can save characters with def factorial(n):return 1 if n<1 else n*factorial(n-1) – Rainbolt Jun 6 '14 at 21:46 • Also, in Python, 0 is equivalent to false when used as a condition, so you could literally say if n: instead of if n < 1: – Rainbolt Jun 6 '14 at 21:49 • You seem to have missed the point of the challenge, as this does not seem the least bit golfed, i.e., written such that the code has as few characters as possible. – Wrzlprmft Jun 6 '14 at 22:11 # Flobnar, 19 bytes |\@<: 1&:: <>-* 1  Try it online! An interesting sibling to Befunge. This uses the -d flag to enable decimal input. ### Explanation: This is basically equivalent to the recursive function: def f(x): if x == 0: return 1 else: return x*f(x-1)  The program starts from the @, and evaluates to its left. The \ evaluates underneath itself and stores that in the call stack. Underneath it is the &, which gives decimal input. If EOF has been reached, it evaluates underneath itself. Underneath, the > pushes the pointer right and returns the top value of the call stack minus 1. That's this section:  \@ &: >- 1  The \ basically counts down from the input and stores the latest version in the call stack. After that, it hits the |, which evaluates further along, and returns either the value above it if the evaluated value is non-zero, else the value below it. The : returns the top of the call stack, so if it is zero, the | goes down and returns 1, else it goes up and wraps around the field. It hits the < which wraps it around again, and then multiplies the top of the call stack by the next iteration of the function. | <: 1 : < *  • You beat me to this answer and probably outgolfed any attempt I might have made! Looks like the -d flag is misdocumented as -e - that should be fixed soon. – Esolanging Fruit Aug 7 '18 at 20:41 # dc, 23 22 bytes 1r[dk*K1-d0<a]dsax_1*+  ### Explanation 1r[dk*K1-d0<a]dsax_1*+ # input on stack, eg: 4 1 # push 1: 1 4 r # reverse top two: 4 1 [dk*K1-d0<a] # push [string]: [string] 4 1 dsa # copy top to register a: [string] 4 1 x # exec top* 0 24 _1 # push -1: -1 0 24 * # multiply: 0 24 + # add: 24 # first exec of [string]: [dk*K1-d0<a] # stack: 4 1 d # duplicate top: 4 4 1 k # pop & set precision: 4 1 * # multiply 4 K # push precision 4 4 1 # push 1 1 4 4 - # subtract 3 4 d0<a # if top > 0 exec content of register a, namely [string] # output on stack: 24  In case of 0 after the exec part (*), the stack will be -1 0: x # exec top* -1 0 _1 # push -1: -1 -1 0 * # multiply: 1 0 + # add: 1  # µ6, 14 bytes #+[#.[#/0[+/1]/1/2][+/0]/1]  Try it online! ## Explanation # -- primitive recursion with + -- | base case: successor (of 0) [ -- | compose #.[#/0[+/1]/1/2] -- | | multiplication [+/0] -- | | successor of first argument /1 -- | | second argument ] -- | : f n * f (n-1)  # AsciiDots, 805753494847 43 bytes /{*}<$#&
\<^\*#1\
\1#~*{-}-\
/#1/\*.>#?)
.


Outgolfs the sample by 34 57 61 65 66 67 71 bytes. Try it online!

# Powershell, 21 byte

1..$_-ge1-join'*'|iex  Test script: $f = {
1..$_-ge1-join'*'|iex } @( 0,1,2,3,4,5,6,125 ) | % { &$f $_ }  Output: 1 1 2 6 24 120 720 1.88267717688893E+209  ## Explanation 1. 1..$_ generates a sequence of integer from 1 to argument (sequence=(1,0) if argument equal to 0)
2. -ge1 leaves in the sequence numbers great or equal to 1
3. -join'*' converts the sequence to string with '*' between elements
4. |iex evaluates string as powershell expression

# Muriel, 128 bytes

C:"\";@%(B+\");B:\\\"\"+B+\"*\"+$a+\"\\\";a:\"+$(a-1)+\";C:\\\"\"+|C+C),(a>0)*(&B+2),a*999+&B+2";@"B:\".$(1\";a:"+~+";C:\""+|C+C  Try it online! This is a difficult for a number of reasons. First is Muriel itself, where the only way to do loops is to create a quine and eval it, passing a condition into the code. Next is that large numbers lose precision over time (so the output for $$\125!\$$ is 1.88267717688893e+209), but the program can't handle that format inside the program itself, so you can't pass large numbers to the next iteration of code. The last problem is that numbers will eventually be so large, the program just renders them as Inf, but thankfully that's far beyond $$\125!\$$, so we don't have to worry about that. ### Explanation: The first iteration doesn't have to be a complete quine, but can take shortcuts in constructing the actual loop. C:"\";@%(B+\");B:\\\"\"+B+\"*\"+$a+\"\\\";a:\"+$(a-1)+\";C:\\\"\"+|C+C),(a>0)*(&B+2),a*999+&B+2";  This creates an string version of the loop. This string persists across all iterations of the loop. @ # Evaluate the next strings as a new program "B:\".$(1\";       # Initialise B as the string ".$(1" a:"+~ # Initialise a as the inputted number +"C:\""+|C # Initialise C as the persistent string C +C # And add the executing part of the program  The resulting program with input 125 looks like (newlines added for clarity) B:".$(1";
a:125;
C:"\";@%(B+\");B:\\\"\"+B+\"*\"+$a+\"\\\";a:\"+$(a-1)+\";C:\\\"\"+|C+C),(a>0)*(&B+2),a*999+&B+2";
@%(B+");B:\""+B+"*"+$a+"\";a:"+$(a-1)+";C:\""+|C+C),(a>0)*(&B+2),a*999+&B+2


The eval then executes

@                   # Evaluate
%(     ...     ),(a>0)*(&B+2),a*999+&B+2   # The substring
B+");                                    # If a is 0, evaluate B
# Otherwise
B:\""+B+"*"+$a+"\"; # Concatonate "*a" to the end of B a:"+$(a-1)+";                            # Decrement a
C:\""+|C                                 # Set C to C
+C                                       # And add the executing part of the program


Eventually, a will reach 0, and B will be evaluated. B will look like:

.                         # Print
\$(       ...         )   # The string form of
1*125*124*123*...*1    # The factorial


# CJam, 10 bytes

qi1{_(j*}j


Explanation:

qi1{_(j*}j
qi            e# Read input as integer
{    }j    e# Define a recursive function
1           e# Where the value for f(0) = 1
_         e# For f(i), duplicate i
(        e# Then subtract 1
j       e# Run the function for this number (i-1)
*      e# Multiply i and f(i-1) together
e# Implicit output


Try it online!

# MathGolf, 3 bytes

╒ε*


Try it online!

## Explanation

╒    create 1-based range [1, ..., input]
ε*  Reduce by multiplication
`