# Factorial

The factorial of a number is itself and all positive integers less than it multiplied together. It is commonly used as a code example for esoteric programming languages.

It can be defined as $n!=\prod _{i=1}^{n}i$ , which is the form of most implimentations. However, defining this recursively (the main method of coding factorials), we find that $a_{1}=1$ and $a_{n}=na_{n-1}$ , where $a_{n}=n!$ . Using this, we can find that ${\frac {(n+1)!}{n+1}}=n!$ , showing that $0!=1$ . We can also see that negative integers will be undefined due to division by 0.

The factorial function can also be generalised for noninteger reals and even complex numbers via the gamma function $\Gamma (z)\,$ . Note that $\Gamma (n)=(n-1)!\,,$ not $\Gamma (n)=n!\,$ .

For numbers with a real component strictly greater than 0, $\Gamma (z)=\int _{0}^{\infty }t^{z-1}e^{-t}\,dt,\,,$ a fact discovered by Daniel Bernoulli. Another definition, discovered by Euler because of course it was, is ${\frac {1}{z}}\prod _{n=1}^{\infty }\left[{\frac {1}{1+{\frac {z}{n}}}}\left(1+{\frac {1}{n}}\right)^{z}\right],$ which works for all complex numbers other than negative real integers. However, Bernoulli's definition is more generally accepted. Using ${\frac {(n+1)!}{n+1}}=n!$ repeatedly, complex numbers with real components at most 0 can be found if using Bernoulli's definition.

## Implentations

### )

]!|a|
“»€a€,==,0«|§’1’’
§’@‘!\»€a€,-,1«’’
]

### 1+

.111##^"/*\1+\<1+#

### AGSPL

~!.

### Behaviour

factorial = &[1|a<2 a * factorial:a-1]

### Brainfact

*.

Replacing "*" with n +s.

### Campbell

Every program calculates the factorial function.

### Clip

r*,On

Explained as:

r   .- reduce                     -.
*  .- with multiplication        -.
,   .- the list of numbers from   -.
O   .- 1           to             -.
n   .- the number represented by  -.

[implicit] the next line of input.

### dcScript

[NO ERRLOG]
title "Factorial - dcScript implementation"
int fact
int cpt
int n
int c
fac:
defi fact 1
defi cpt 2
start:
ifmin cpt n
mult fact cpt
call start
ifend
ifeql cpt n
mult fact cpt
call start
ifend
sysprt "x! = "
dspvar int fact
sysprtn ""
_start:
sysprt "Entrez le x = "
getstream int n
ifeql n -1
call end
ifend
call fac
end:
sysprtn "x! = 1"
sysprtn "Press any key to continue..."
wait
quit

### Flow chart

(Contains other showcases) ### FunctionsFTW

 setNum("input", strtoNum(input()));                           "Get number";
setNum("result", 1);                                          "The result";

label("l");
if(gt(getNum("input"), 1), () {                               "If input > 1";
setNum("result", mult(getNum("result"), getNum("input")));    "result = result * input";
goto("l");                                                    "Repeat";
}, () {});

print(numToStr(getNumber("result")));                         "Print result";


Alternatively, golfed:

setNum("i",strtoNum(input()));setNum("r",1);label("l");if(gt(getNum("i"),1),(){setNum("r",mult(getNum("r"),getNum("i")));setNum("i",add(getNum("i"),neg(1)));goto("l");},(){});print(numToStr(getNumber("r")));


### gar

Works only with numbers greater than 2.

>
-
:
?
[
:
<
+
]
~
~
[[
*
&
]]
~
!

gar

### Gofe

,&t1&s[#sMt-&s#t].


### GotoScript

# Get input
1 n := max := INPUT

# Factorial operation
2 n *= GOTOS + 1

# End loop
3 GOTO 5 IF GOTOS >= max - 2

# Continue loop
4 GOTO 2

# Output
5 PRINT n


### HGFTSNOA

This program calculates the factorial of the number on the second line.

T
t 30
TT
t TTTTT ttt T
TTT ttt TTTTT 0
0
TTTTTTT
ttttttt ttt 1 1
TTTTTT
TT
ttttttt TTTTT ttt tttt ttttt T
TTT ttt TTTTT tttt
TTT 0 TTTTTT
TTTT tt ttttt tttt 0 0
TTTTTTT
TTTT ttttttt ttt tttt TTTTTTTT 1 tt ttttt tttt 0 0
TTTTTT
TT
tt TTTTT ttt tttt ttttt tttttt T
TTT ttttt TTTTT tttt
TTT 0 TTTTTT
TTTT tttttt
TTTTTTT
TTTT tt ttt tttt ttttt TTTTTTTT 1 tttttt TTTTTTTT ttt
TTTTTT
TT


### J--

main{
echo(factorial(n));
}

Replacing "n" with the number of your choice.

### kS

FUNC factorial $to 1 >$exit
$to >$i > LOOP
$exit *$i > $exit$i - 1 > $i END$exit > RETURN

Classified

### Marble Machine

\# o
o\2/
~ g
&~{~
*~ ~
&  1
~  -
&  &
\~o~
&~x
1\~
g ~
~}x.
x  !

### Osis

{*1

### Turing (Iamcalledbob)

 % Accepts a number and calculates its factorial
fun factorial (n)
if n = 0
result 1
else
result n * factorial (n - 1)

fun loop()
get n
if n < 0
put "The factorial of ", n, " is ", factorial (n)
result n
else
result loop()

loop()


### Upsilon

factorial outvar result, a:
equal isZero, a, 0.
subtract x, a, 1.
if isZero:
assign result, 1.
else:
factorial result, x.
end.
back.

### x(y)

factorial(n)
-> (n < 2){1 n * {n - 1}}

### Zull

~`

User must wish for a factorial program.