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

154 Answers 154

1 2 3 4 5
6
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\$
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\$
-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\$
    – Jo King
    Commented May 7, 2023 at 2:44
  • 1
    \$\begingroup\$ 84 bytes \$\endgroup\$
    – The Thonnu
    Commented May 7, 2023 at 13:50
1 2 3 4 5
6

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.