58
\$\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\$

161 Answers 161

1 2 3 4 5
6
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\$

Python, 26 Bytes

import math math.factorial 

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

\$\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\$

HP‑41C series, 1 B

Place \$n\$ into the X register (that is the top of the stack) and XEQ (execute) the built‑in command

FACT 

\$n!\$ is now in the X register and \$n\$ in the L (“last X”) register. The operation reports a DATA ERROR in case of negative or non‑integral arguments, and an OUT OF RANGE error for \$n \ge 70\$.

You can ignore “out of range” errors by setting flag 24 (SF 24). If flag 24 is set, the operation is successful and yields 9.999999999 × 1099 as an “approximation”.

\$\endgroup\$
1
  • \$\begingroup\$ FACT indeed, heh \$\endgroup\$ Commented Dec 20, 2024 at 4:38
0
\$\begingroup\$

Pascal, 27 B

The source code is split into a left half containing the boilerplate for a complete Pascal program, and right half containing the necessary step to evaluate \$n!\$ (i. e. the task). Only the extra bytes necessary to achieve the task are counted.

{ boilerplate }{ necessary for evalution } program factorial(input, output); var n ,i :integer; begin readLn(input, n); for i≔2 to n−1 do n≔i*n ;writeLn(output, n) end. 

The built‑in data type integer comprises (at least) the range −maxInt through +maxInt inclusive. The value of the built‑in constant maxInt is implementation‑defined. Today, it is usually \$2^{63}-1\$, so you can calculate up to \$20!\$.

Remember that in Pascal for‑loop limits are inclusive, so you subtract \$1\$ from \$n\$. This is similar to the limits in mathematical notation \$\prod\$ and \$\sum\$.

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

jBasher2, 180 bytes

create a with type number create b with type number ask for input set that to a set 1 to b while a > 1 multiply b by a set that to b subtract a by 1 set that to a endwhile output b 

very self explanatory

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

h6, 21 bytes

{1$range!{1+*}lfold!} 

Explanation:

1$ # put 1 behind n range! # take the range 0..n {1+*}lfold! # fold the range, starting at 1, by # multiplying the accumulator by one plus # the value from the range. 

Alternatively, with just plain recursion, 22 bytes:

f:{.1>{;1}{.1-f!*}l?!} 
\$\endgroup\$
0
\$\begingroup\$

AArch64 machine code, 24 bytes

Completely port of ARM Thumb-2 answer except input and output are registers x1, x0:

Disassembly of section .text: 0000000000000000 <f>: 0: d2800020 mov x0, #0x1 // #1 4: b4000081 cbz x1, 14 <f+0x14> 8: 9b017c00 mul x0, x0, x1 c: d1000421 sub x1, x1, #0x1 10: b5ffffc1 cbnz x1, 8 <f+0x8> 14: d65f03c0 ret 

Source

/// fn (x1: u64) x0: u64 .globl f f: mov x0,1 cbz x1,99f 1: mul x0,x0,x1 sub x1,x1,1 cbnz x1,1b 99: ret 

How I tested on Termux

$ cat main.c #include <stdio.h> #include <stdlib.h> unsigned long long f(int, unsigned long long); int main(int argc, char **argv) { unsigned long long x = strtoull(argv[1], NULL, 10); printf("%llu\n", f(0, x)); } $ clang main.c f.s $ for x in $(seq 0 12); do printf "%2d => " "$x"; ./a.out "$x"; done 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 $ 
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 23 bytes

#define f(x)tgamma(x+1) 

This is the obvious built-in function.

Try it online!

\$\endgroup\$
-3
\$\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\$ Commented May 6, 2023 at 22:12
  • 3
    \$\begingroup\$ you could golf this a little more, e.g. removing whitespace and uneeded variables \$\endgroup\$ Commented May 7, 2023 at 2:44
  • 1
    \$\begingroup\$ 84 bytes \$\endgroup\$ Commented May 7, 2023 at 13:50
1 2 3 4 5
6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.