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}^ni, 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.
==Implementations==
===)===
]!|a|
 “»€a€,==,0«|§’1’’
 §’@‘!\»€a€,-,1«’’
]
===1+===
.111##^"/*\1+\<1+#
===AGSPL===
~!.
===Behaviour===
factorial = &[1|a<2 a * factorial:a-1]
===Brainfact===
<nowiki>*</nowiki>.
Replacing "*" with n +s.
===Campbell===
Every program calculates the factorial function.
===ChromaCode===
===Clip===
r*`,On
Explained as:
r .- reduce -.
 <nowiki>*</nowiki>` .- 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
addi cpt 1
call start
ifend
ifeql cpt n
mult fact cpt
addi cpt 1
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
===FALSE===
 [1\[$1>][$@*\1-]#%]f:
 5f;!.
===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";
 setNum("input", add(getNum("input"), neg(1))); "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.
===jive===
 *`,!↓
====Implementations with automatic type conversion====
 *!↓
===kS===
FUNC factorial $to
 1 > $exit
 $to > $i > LOOP
 $exit * $i > $exit
 $i - 1 > $i
 END
 $exit > RETURN
===Mao===
Classified
===Marble Machine===
\# o
o\2/
~ g
&~{~
*~ ~
& 1
~ -
& &
\~o~
 &~x
 1\~
 g ~
~}x.
x !
===MEMORYLEEK===
point f; vnput f;
point total; total = 1;
until f == 0;
 point r; r ~> -1; #r; :r;
 total * f; #total; total ~> -1;
 f - 1; #f; f ~> -1;
 point r; r ~> -2; #r; :r; 
back;
point r; r ~> -1; #r; :r;
!total; #total; :total; #f; :f;
===MoreMathRPN===
 inputR
 floor
 >> 0
 outputV
 outputS "! ="
 1
 repeat ]1
 >>> 0
 *
 next
 outputV
===Osis===
`{*1
===Recs===
(let
 omega
 (lam x(x x))
 fix
 (omega
 (lam f
 (lam x
 (x((f f)x)))))
 fact
 (fix
 (lam fact
 (lam x
 (if x
 (* x(fact(- x 1)))
 1))))
 (fact 10)
)
Or by using primitive recursion
(let
 fact
 (R (C S Z) (C * (C S (P 2 1)) (P 2 2)))
 (fact 10)
)
===Trampolines===
|o #
|,"Factorial: " #
|} #
|1 \ #
|{H==============H #
|.H H #
|=H- }; H #
| \ 0 ^ # H #
| > ]} 1! H #
| ^ ~ * { +H #
| - - H #
| - #
===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.
===WhatLang===
This program calculates factorial of input and prints the result
 ¿(:~[(:1-fact^@*)(_1)]\,\_@)fact=_
 pr@num@fact^@.
Without recursive:
 ¿(range@(1+repr@(*)+)#\_()join@\_(1 )+@)fact=_
 pr@num@fact^@
Another one without recursive:
 ¿(range@(1+)#\_(*)join@\_(1 )\+(*)+@)fact=_
 pr@num@fact^@
===x(y)===
factorial(n)
-> (n < 2){1 n * {n - 1}}
===Zull===
~
User must wish for a factorial program.
Category:Program forms