54
\$\begingroup\$

Task

Given a non-negative integer \$n\$, evaluate the factorial \$n!\$.

The factorial is defined as follows:

$$ n!=\begin{cases}1 & n=0\\n\times(n-1)!&n>0\end{cases} $$

Rules

  • All default I/O methods are allowed.
  • Standard loopholes are forbidden.
  • Built-ins are allowed.
  • There is no time or memory limit.
  • Giving imprecise or incorrect results for large inputs due to the limit of the native number format is fine, as long as the underlying algorithm is correct. Specifically, it is not allowed to abuse the native number type to trivialize the challenge, which is one of the standard loopholes.
  • This is . Shortest code in bytes wins, but feel free to participate in various esolangs (especially the ones hindered by the restrictions of the former challenge).

Test cases

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600

Note: We already have the old factorial challenge, but it has some restrictions on the domain, performance, and banning built-ins. As the consensus here was to create a separate challenge without those restrictions so that more esolangs can participate, here it goes.

Also, we discussed whether we should close the old one as a duplicate of this, and we decided to leave it open.

\$\endgroup\$

146 Answers 146

1 2 3 4
5
1
\$\begingroup\$

Knight, 26 bytes

;=yT;=x+0P;W=x-xT=y*+xTyOy

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 24 bytes: ;=x+=y 1P;W=x-xT=y*x yOy \$\endgroup\$
    – Aiden Chow
    Aug 14, 2022 at 23:21
1
\$\begingroup\$

StackCell, 14 bytes

Uses this input format and this output format

The below code uses the Unicode control character glyphs [U+24XX] to represent ASCII unprintable control characters embedded in the source code

[the input is implicitly pushed to the stack before the program begins]

{'␁}[}*'␁}-{}]

[the output is left as the sole value on the stack after the program ends]

For standard I/O (i.e. stdin and stdout), the following snippet can be used to place the input on the stack (for 21 bytes):

'0'␁['0x-x'
*+@:'
-]`

and for output (26 bytes):

:[:'
x%'0+{X}X'
x/:]X:[:;]

For the complete program using stdin/stdout only, that means a total of 61 bytes:

'0'␁['0x-x'
*+@:'
-]`{'␁}[}*'␁}-{}]:[:'
x%'0+{X}X'
x/:]X:[;:]
\$\endgroup\$
1
\$\begingroup\$

Carbon, 53 bytes

fn f(x:i32)->i32{return if(x==0)then 1else x*f(x-1);}

Try it Online!

Here is a full program for testing at the above site:

package c api;

fn f(x:i32)->i32{return if(x==0)then 1else x*f(x-1);}

fn Main() -> i32 {
  return f(5);
}
\$\endgroup\$
1
\$\begingroup\$

Haskell, 17 characters

f n=product[1..n]

Bonus reference: "The Evolution of a Haskell Programmer" by Fritz Ruehr

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Clojure, 38 bytes

(defn f[n](apply *' (range 2(inc n))))

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 36 \$\endgroup\$
    – ASCII-only
    May 7 at 4:18
1
\$\begingroup\$

Rattle, 14 bytes

|F0:s[1F]-F0*~

Try it Online!

There's already a Rattle answer here by me but this approach is completely different. Not only does this answer use the shiny new interpreter, but it takes advantage of the fact that recursion was recently implemented in Rattle!

Explanation

|                parses the user's input
 F0              calls local function 0
   :             (separator between the main method and local function 0)
    s            save the current value to local memory slot 0 (local to only this instance of F0)
     [1 ]        if the value is equal to 1, then:
       F         return (returns 1)
         -       subtract 1 from the current value
          F0     recursively call function 0 with the current value as a parameter
            *~   multiply the result of function 0 by the value stored in local memory
                 (the result of the multiplication is returned)

Rattle is able to process factorials up to 170! with no loss in precision.

\$\endgroup\$
1
\$\begingroup\$

Thunno, \$ 1 \log_{256}(96) \approx \$ 0.82 bytes

F

Attempt This Online! or verify \$0!\$ to \$10!\$.

Builtins FTW.

\$\endgroup\$
1
\$\begingroup\$

Pyt, 1 byte

!

Try it online!

Builtins FTW.

\$\endgroup\$
1
\$\begingroup\$

Arturo, 9 bytes

factorial

Try it

Builtin

Arturo, 18 12 bytes

$=>[∏1..&]

Try it

Non-builtin

\$\endgroup\$
1
\$\begingroup\$

Nibbles, 8 nibbles (4 bytes)

? $ `*,$ 1

Attempt This Online!

Explanation

? $ `*,$ 1    #
----------------------------------------
? $           # if arg1
              # equals truthy (<0)
    `*        #    product
      ,$      #    range 1..arg1
              # else
         1    #    1 
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 9 bytes

factorial

Try it online!

12 bytes

~x=*(1:x...)

Try it online!

~x=prod(1:x)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

vemf, 1 byte

!

Falls back to \$\Gamma\left(\alpha+1\right)\$ if \$\alpha\notin\mathbb Z\$

In the online interpreter this works (returns values other than /0/-0/inf) for \$-178.9999999999999\le\alpha\le170.6243769563\$ and \$\alpha\notin\mathbb Z^-\$ (set of negative integers)

\$\endgroup\$
1
\$\begingroup\$

All answers works correctly for 0 only in Firefox

JavaScript, 29 bytes

  • Without recursion
  • Expects Number type
  • Max value is 170. If above outputs Infinity
n=>eval('p=1;while(n)p*=n--')

f=n=>eval('p=1;while(n)p*=n--')

;[
  0, // 1
  1, // 1
  2, // 2
  3, // 6
  4, // 24
  5, // 120
  6, // 720
  7, // 5040
  8, // 40320
  9, // 362880
  10, // 3628800
  11, // 39916800
  12, // 479001600
  170, // 7.257415615308004e+306
  171, // Infinity
].forEach(n=>document.write(n + ' - ' + f(n), '<br>'))

JavaScript, 30 bytes

  • Without recursion
  • Expects BigInt type
  • In theory there is no max value. In practice it depends on implementation
n=>eval('p=1n;while(n)p*=n--')

f=n=>eval('p=1n;while(n)p*=n--')

;[
  0n, // 1
  1n, // 1
  2n, // 2
  3n, // 6
  4n, // 24
  5n, // 120
  6n, // 720
  7n, // 5040
  8n, // 40320
  9n, // 362880
  10n, // 3628800
  11n, // 39916800
  12n, // 479001600
  170n, // 7257415615307998967396728211129263114716991681296451376543577798900561843401706157852350749242617459511490991237838520776666022565442753025328900773207510902400430280058295603966612599658257104398558294257568966313439612262571094946806711205568880457193340212661452800000000000000000000000000000000000000000
  171n, // 1241018070217667823424840524103103992616605577501693185388951803611996075221691752992751978120487585576464959501670387052809889858690710767331242032218484364310473577889968548278290754541561964852153468318044293239598173696899657235903947616152278558180061176365108428800000000000000000000000000000000000000000
].forEach(n=>document.write(n + ' - ' + f(n), '<br>'))

JavaScript, 33 bytes

  • Without recursion
  • Expects Number or BigInt type
  • Max value for type Number is 170. In theory there is no max value for type BigInt
n=>eval('p=++n/n;while(--n)p*=n')

f=n=>eval('p=++n/n;while(--n)p*=n')

;[
  0, // 1
  1, // 1
  2, // 2
  3, // 6
  4, // 24
  5, // 120
  6, // 720
  7, // 5040
  8, // 40320
  9, // 362880
  10, // 3628800
  11, // 39916800
  12, // 479001600
  170, // 7.257415615308004e+306
  171, // Infinity
  0n, // 1
  1n, // 1
  2n, // 2
  3n, // 6
  4n, // 24
  5n, // 120
  6n, // 720
  7n, // 5040
  8n, // 40320
  9n, // 362880
  10n, // 3628800
  11n, // 39916800
  12n, // 479001600
  170n, // 7257415615307998967396728211129263114716991681296451376543577798900561843401706157852350749242617459511490991237838520776666022565442753025328900773207510902400430280058295603966612599658257104398558294257568966313439612262571094946806711205568880457193340212661452800000000000000000000000000000000000000000
  171n, // 1241018070217667823424840524103103992616605577501693185388951803611996075221691752992751978120487585576464959501670387052809889858690710767331242032218484364310473577889968548278290754541561964852153468318044293239598173696899657235903947616152278558180061176365108428800000000000000000000000000000000000000000
].forEach(n=>document.write(n + ' (' + typeof n + ') - ' + f(n), '<br>'))

\$\endgroup\$
6
  • \$\begingroup\$ Fails for input 0 \$\endgroup\$
    – Shaggy
    Mar 7 at 23:26
  • \$\begingroup\$ @Shaggy I already added 0 as test cases and output is 1 everywhere \$\endgroup\$
    – EzioMercer
    Mar 7 at 23:29
  • \$\begingroup\$ @EzioMercer What browser did you test this on? I'm on a chromium-based browser and f(0) is returning undefined for all three versions \$\endgroup\$
    – noodle man
    Jul 14 at 18:47
  • \$\begingroup\$ @noodleman Firefox 115.0.2 (64-bit) \$\endgroup\$
    – EzioMercer
    Jul 15 at 18:15
  • 1
    \$\begingroup\$ I get the same f(0) === undefined on Safari on my iPhone. Can you double check that you are getting the expected 1? If you are then you might want to add that it only works on Firefox to your answer. I see why it might return 1 if the result of p=1 is returned, since the result of the while loop would never happen, but it’s probably implementation dependent or a bug \$\endgroup\$
    – noodle man
    Jul 15 at 19:52
1
\$\begingroup\$

Itr, 2 bytes:

#P

online interpreter

The product over the range from 1 to the input number

\$\endgroup\$
1
\$\begingroup\$

Uiua, 5 bytes

/×+1⇡

Try it!

    ⇡  # create range [0..input)
  +1   # amend it to [1..input]
/×     # reduce by multiplication
\$\endgroup\$
0
\$\begingroup\$

PPL, 60 bytes

fnf(n){
declarea=1
declares=1
loopn{
a=a*s
s=s+1
}
returna
}

Anybody remember PPL? ;)

Declares a new function f with variables a and s. Loops n times and multiplies a by s each time.

Sadly recursion is not supported with PPL.

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

APOL, 24 bytes

⊕(ƒ(-(⧣ 1) *(⋒ -(⋒ ∈))))

Explanation:

⊕(         Sum of list
  ƒ(        List-builder for
    -(      Subtract
      ⧣     Integer input
      1
    )
    *(      Multiply
      ⋒     For iterator (what's being iterated through)
      -(    Subtract
         ⋒  For iterator
         ∈  Loop counter
      )
    )
  )
)
\$\endgroup\$
0
\$\begingroup\$

Python, 34 29 Bytes

x=lambda a:a and a*x(a-1)or 1

lambda is good at shortening code!
-5 from TheThonnu

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Your function us recursive so you need to name it by putting x= in front \$\endgroup\$
    – The Thonnu
    Apr 5 at 16:23
  • \$\begingroup\$ 29 bytes \$\endgroup\$
    – The Thonnu
    Apr 5 at 16:24
0
\$\begingroup\$

Fortran (GFortran), 51 42 bytes

We abuse that variables (and functions) beginning with i,j,k,l,m or n are assumed to be (or return) integers, and that all other objects are assumed to be (or return) reals to create this function that generates a list of integers and multiplies them together into a another integer. It breaks at i=13.

function k(i)
k=product((/(j,j=1,i)/))
end

Try it online!

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

minigolf, 6 bytes

1i,n*_

Attempt This Online!

Explanation

1      Push 1
i      Push input
,      Repeat input times ([1..n]):
  n*     Muliply tos by curr. item
_      end repeat

implicit output
\$\endgroup\$
0
\$\begingroup\$

Python, 26 Bytes

import math
math.factorial

Uses the built-in function from the built-in module math.

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

Thunno 2, 1 byte

w

Attempt This Online! Built-in.

Thunno 2, 2 bytes

Rp

Attempt This Online! Product of range.

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

Desmoslang Assembly, 3 Bytes

I!OT

Explanation: Takes input to Command, adds an exclamation mark for factorial, outputs it, and loops infinitely at the end.

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

Thue++, 43 bytes

^(x*)x!::=$1!/$1
^((!+)/+x*)x::=$1$2
/!::=!

Input is on the state, in the form of some number of xs followed by an exclamation mark, output is on the state in the form of a number of exclamation marks.

Alternate idea that I'm not sure if allowed because the output has a number of slashes that arent part of the output total, 37 bytes:

^(x*)x!::=$1!/$1
^(([/!]+)x*)x::=$1$2
\$\endgroup\$
0
\$\begingroup\$

Swift, 37 bytes

var f={$0==0 ?1:(1...$0).reduce(1,*)}

Self-explanatory. Call it as f(n).

\$\endgroup\$
-2
\$\begingroup\$

Python + sympy, 94 Bytes

from sympy import *
x=Symbol("x")
i=int(input())
e=x**i
for y in range(i):
 e=diff(e)
print(e)

Requires you to do pip install sympy in the command prompt, as this isn't a built-in module. Uses a calculus approach to calculate factorials.

\$\endgroup\$
3
  • 4
    \$\begingroup\$ You should label this as Python + sympy \$\endgroup\$
    – noodle man
    May 6 at 22:12
  • 3
    \$\begingroup\$ you could golf this a little more, e.g. removing whitespace and uneeded variables \$\endgroup\$
    – Jo King
    May 7 at 2:44
  • 1
    \$\begingroup\$ 84 bytes \$\endgroup\$
    – The Thonnu
    May 7 at 13:50
1 2 3 4
5

Your Answer

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

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