Factorial: Difference between revisions

8,401 bytes added ,  30 days ago
(Jakt)
(47 intermediate revisions by 26 users not shown)
Line 362:
endif.
endform.</syntaxhighlight>
 
=={{header|Acornsoft Lisp}}==
 
===Recursive===
<syntaxhighlight lang="lisp">(defun factorial (n)
(cond ((zerop n) 1)
(t (times n (factorial (sub1 n))))))
</syntaxhighlight>
 
===Iterative===
<syntaxhighlight lang="lisp">(defun factorial (n (result . 1))
(loop
(until (zerop n) result)
(setq result (times n result))
(setq n (sub1 n))))
</syntaxhighlight>
 
=={{header|Action!}}==
Line 542 ⟶ 558:
 
=={{header|Agda}}==
<syntaxhighlight lang="agda">factorialmodule :Factorial ℕ → ℕwhere
 
open import Data.Nat using (ℕ ; zero ; suc ; _*_)
 
factorial : (n : ℕ) → ℕ
factorial zero = 1
factorial (suc n) = (suc n) * (factorial n</syntaxhighlight>)
</syntaxhighlight>
 
=={{header|Aime}}==
Line 729 ⟶ 750:
<syntaxhighlight lang="apl"> FACTORIAL 6
720</syntaxhighlight>
{{works with|Dyalog APL}}
A recursive definition is also possible:
<syntaxhighlight lang="apl">
fac←{⍵>1 : ⍵×fac ⍵-1 ⋄ 1}
fac 5
120
</syntaxhighlight>
 
=={{header|AppleScript}}==
Line 1,144 ⟶ 1,172:
end // end of [factorial]
</syntaxhighlight>
 
=={{header|Asymptote}}==
===Iterative===
<syntaxhighlight lang="Asymptote">real factorial(int n) {
real f = 1;
for (int i = 2; i <= n; ++i)
f = f * i;
return f;
}
 
write("The factorials for the first 5 positive integers are:");
for (int j = 1; j <= 5; ++j)
write(string(j) + "! = " + string(factorial(j)));</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 1,294 ⟶ 1,335:
}
</syntaxhighlight>
 
===Imperative===
<syntaxhighlight lang="bash">factorial()
{
declare -nI _result=$1
declare -i n=$2
 
_result=1
while (( n > 0 )); do
let _result*=n
let n-=1
done
}
</syntaxhighlight>
 
(the imperative version will write to a variable, and can be used as <code>factorial f 10; echo $f</code>)
 
=={{header|BASIC}}==
Line 1,488 ⟶ 1,545:
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">'factorialaccurate examplebetween 1-12
'accurate between 1-12
 
print "version 1 without function"
let f = 1
 
for i = 1 to 12
alert "factorial"
input "enter an integer: ", n
 
let n = i
do
let f = 1
 
do
let f = f * n
let n = n - 1
 
let f = f * n
loop n > 0
let n = n - 1
 
loop n > 0
print f
 
print f, " ",
end</syntaxhighlight>
wait
 
next i
 
print newline, newline, "version 2 with function"
 
for i = 1 to 12
 
print factorial(i), " ",
 
next i</syntaxhighlight>
{{out| Output}}<pre>version 1 without function
1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600
 
version 2 with function
1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600 </pre>
 
==={{header|FreeBASIC}}===
Line 1,568 ⟶ 1,640:
end</syntaxhighlight>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1, @"Factorial", ( 0, 0, 300, 550 )
 
local fn factorialIterative( n as long ) as double
double f
long i
 
if ( n > 1 )
f = 1
for i = 2 to n
f = f * i
next
else
f = 1
end if
end fn = f
 
local fn factorialRecursive( n as long ) as double
double f
 
if ( n < 2 )
f = 1
else
f = n * fn factorialRecursive( n -1 )
end if
end fn = f
 
long i
 
for i = 0 to 12
print "Iterative:"; using "####"; i; " = "; fn factorialIterative( i )
print "Recursive:"; using "####"; i; " = "; fn factorialRecursive( i )
print
next
 
HandleEvents</syntaxhighlight>
 
Output:
<pre>
Iterative: 0 = 1
Recursive: 0 = 1
 
Iterative: 1 = 1
Recursive: 1 = 1
 
Iterative: 2 = 2
Recursive: 2 = 2
 
Iterative: 3 = 6
Recursive: 3 = 6
 
Iterative: 4 = 24
Recursive: 4 = 24
 
Iterative: 5 = 120
Recursive: 5 = 120
 
Iterative: 6 = 720
Recursive: 6 = 720
 
Iterative: 7 = 5040
Recursive: 7 = 5040
 
Iterative: 8 = 40320
Recursive: 8 = 40320
 
Iterative: 9 = 362880
Recursive: 9 = 362880
 
Iterative: 10 = 3628800
Recursive: 10 = 3628800
 
Iterative: 11 = 39916800
Recursive: 11 = 39916800
 
Iterative: 12 = 479001600
Recursive: 12 = 479001600
</pre>
 
==={{header|Gambas}}===
Line 1,991 ⟶ 1,985:
80 PRINT F
90 END</syntaxhighlight>
 
==={{header|Tiny Craft Basic}}===
<syntaxhighlight lang="basic">10 let f = 1
 
20 print "factorial"
30 input "enter an integer (1-34): ", n
 
40 rem loop
 
60 let f = f * n
70 let n = n - 1
 
80 if n > 0 then 40
 
90 print f
100 shell "pause"</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 2,387 ⟶ 2,365:
^-1:_$>\:|
@.$<</syntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
Factorial on Church numerals in the lambda calculus is <code>λn.λf.n(λf.λn.n(f(λf.λx.n f(f x))))(λx.f)(λx.x)</code> (see https://github.com/tromp/AIT/blob/master/numerals/fac.lam) which in BLC is the 57 bits
<pre>000001010111000000110011100000010111101100111010001100010</pre>
 
=={{header|BQN}}==
Line 2,468 ⟶ 2,450:
true? x == 0 1 { x * factorial(x - 1)}
}</syntaxhighlight>
 
=={{header|Bruijn}}==
 
Implementation for numbers encoded in balanced ternary using Mixfix syntax defined in the Math module:
 
<syntaxhighlight lang="bruijn">
:import std/Math .
 
factorial [∏ (+1) → 0 | [0]]
 
:test ((factorial (+10)) =? (+3628800)) ([[1]])
</syntaxhighlight>
 
=={{header|Burlesque}}==
Line 3,403 ⟶ 3,397:
 
x: 5</syntaxhighlight>
 
=={{header|Coq}}==
<syntaxhighlight lang="coq">
Fixpoint factorial (n : nat) : nat :=
match n with
| 0 => 1
| S k => (S k) * (factorial k)
end.
</syntaxhighlight>
 
=={{header|Crystal}}==
Line 3,795 ⟶ 3,798:
 
<syntaxhighlight lang="text">
func factorial n . r .
r = 1
for i = 2 to n
r *= i
.
return r
.
callprint factorial 7 r
print r
</syntaxhighlight>
 
Line 3,845 ⟶ 3,848:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module ShowFactorials {
static <Value extends IntNumber> Value factorial(Value n) {
{
static <Value extends IntNumber> Value factorial(Value n)
{
assert:arg n >= Value.zero();
return n <= Value.one() ? n : n * factorial(n-Value.one());
}
 
@Inject Console console;
void run() {
{
// 128-bit test
UInt128 bigNum = 34;
Line 3,861:
 
// 64-bit test
for (Int i : 10..-1) {
{
console.print($"factorial({i})={factorial(i)}");
}
}
}
}
</syntaxhighlight>
 
Line 4,090 ⟶ 4,089:
=={{header|Elm}}==
 
==={{header|Recursive}}===
 
<syntaxhighlight lang="elm">
Line 4,098 ⟶ 4,097:
</syntaxhighlight>
 
==={{header|Tail Recursive}}===
 
<syntaxhighlight lang="elm">
Line 4,110 ⟶ 4,109:
</syntaxhighlight>
 
==={{header|Functional}}===
 
<syntaxhighlight lang="elm">
Line 4,153 ⟶ 4,152:
=>
"265252859812191058636308480000000"</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun iterative = int by int n
int result = 1
for int i = 2; i <= n; ++i do result *= i end
return result
end
fun recursive = int by int n do return when(n <= 0, 1, n * recursive(n - 1)) end
writeLine("n".padStart(2, " ") + " " + "iterative".padStart(19, " ") + " " + "recursive".padStart(19, " "))
for int n = 0; n < 21; ++n
write((text!n).padStart(2, " "))
write(" " + (text!iterative(n)).padStart(19, " "))
write(" " + (text!recursive(n)).padStart(19, " "))
writeLine()
end
</syntaxhighlight>
{{out}}
<pre>
n iterative recursive
0 1 1
1 1 1
2 2 2
3 6 6
4 24 24
5 120 120
6 720 720
7 5040 5040
8 40320 40320
9 362880 362880
10 3628800 3628800
11 39916800 39916800
12 479001600 479001600
13 6227020800 6227020800
14 87178291200 87178291200
15 1307674368000 1307674368000
16 20922789888000 20922789888000
17 355687428096000 355687428096000
18 6402373705728000 6402373705728000
19 121645100408832000 121645100408832000
20 2432902008176640000 2432902008176640000
</pre>
 
=={{header|embedded C for AVR MCU}}==
Line 4,680 ⟶ 4,721:
in out
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
window 1, @"Factorial", ( 0, 0, 300, 550 )
 
local fn factorialIterative( n as long ) as double
double f
long i
 
if ( n > 1 )
f = 1
for i = 2 to n
f = f * i
next
else
f = 1
end if
end fn = f
 
local fn factorialRecursive( n as long ) as double
double f
 
if ( n < 2 )
f = 1
else
f = n * fn factorialRecursive( n -1 )
end if
end fn = f
 
long i
 
for i = 0 to 12
print "Iterative:"; using "####"; i; " = "; fn factorialIterative( i )
print "Recursive:"; using "####"; i; " = "; fn factorialRecursive( i )
print
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Iterative: 0 = 1
Recursive: 0 = 1
 
Iterative: 1 = 1
Recursive: 1 = 1
 
Iterative: 2 = 2
Recursive: 2 = 2
 
Iterative: 3 = 6
Recursive: 3 = 6
 
Iterative: 4 = 24
Recursive: 4 = 24
 
Iterative: 5 = 120
Recursive: 5 = 120
 
Iterative: 6 = 720
Recursive: 6 = 720
 
Iterative: 7 = 5040
Recursive: 7 = 5040
 
Iterative: 8 = 40320
Recursive: 8 = 40320
 
Iterative: 9 = 362880
Recursive: 9 = 362880
 
Iterative: 10 = 3628800
Recursive: 10 = 3628800
 
Iterative: 11 = 39916800
Recursive: 11 = 39916800
 
Iterative: 12 = 479001600
Recursive: 12 = 479001600
</pre>
 
 
=={{header|GAP}}==
Line 5,181 ⟶ 5,303:
return n * factorial(n - 1);
];</syntaxhighlight>
 
=={{header|Insitux}}==
 
{{trans|Clojure}}
 
'''Iterative'''
 
<syntaxhighlight lang="insitux">
(function factorial n
(... *1 (range 2 (inc n))))
</syntaxhighlight>
 
'''Recursive'''
 
<syntaxhighlight lang="insitux">
(function factorial x
(if (< x 2)
1
(*1 x (factorial (dec x)))))
</syntaxhighlight>
 
=={{header|Io}}==
Line 5,899 ⟶ 6,041:
=={{header|langur}}==
=== Folding ===
<syntaxhighlight lang="langur">val .factorial = ffn(.n) fold(ffn{*}, .x x2 .y, pseries. .n)
writeln .factorial(7)</syntaxhighlight>
 
{{works with|langur|0.6.13}}
<syntaxhighlight lang="langur">val .factorial = f fold(f{x}, 2 .. .n)
writeln .factorial(7)</syntaxhighlight>
 
=== Recursive ===
<syntaxhighlight lang="langur">val .factorial = ffn(.x) { if(.x < 2: 1; .x x* self(.x - 1)) }
writeln .factorial(7)</syntaxhighlight>
 
=== Iterative ===
<syntaxhighlight lang="langur">val .factorial = ffn(.i) {
var .answer = 1
for .x in 2 .. .i {
.answer x*= .x
}
.answer
}
 
writeln .factorial(7)</syntaxhighlight>
 
=== Iterative Folding ===
<syntaxhighlight lang="langur">val .factorial = fn(.n) { for[=1] .x in .n { _for *= .x } }
{{works with|langur|0.7.0}}
<syntaxhighlight lang="langur">val .factorial = f(.n) for[=1] .x in .n { _for x= .x }
writeln .factorial(7)</syntaxhighlight>
 
Line 5,967 ⟶ 6,105:
acc.
}.</syntaxhighlight>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
n is number
 
procedure:
sub factorial
parameters:
n is number
result is number
local data:
i is number
m is number
procedure:
store 1 in result
in m solve n + 1
for i from 1 to m step 1 do
multiply result by i in result
repeat
end sub
create statement "get factorial of $ in $" executing factorial
 
get factorial of 5 in n
display n lf
</syntaxhighlight>
{{out}}
<pre>
120
</pre>
 
=={{header|Lean}}==
 
<syntaxhighlight lang="lean">
def factorial (n : Nat) : Nat :=
match n with
| 0 => 1
| (k + 1) => (k + 1) * factorial (k)
</syntaxhighlight>
 
=={{header|LFE}}==
Line 6,143 ⟶ 6,319:
// iterative
function factorialit n
put 1 into f
if n > 1 then
repeat with i = 1 to n
Line 7,101 ⟶ 7,277:
4 factorial . ( => 24 )
10 factorial . ( => 3628800 )</syntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
def 'math factorial' [] {[$in 1] | math max | 1..$in | math product}
 
..10 | each {math factorial}
</syntaxhighlight>
{{out}}
<pre>
╭────┬─────────╮
│ 0 │ 1 │
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 6 │
│ 4 │ 24 │
│ 5 │ 120 │
│ 6 │ 720 │
│ 7 │ 5040 │
│ 8 │ 40320 │
│ 9 │ 362880 │
│ 10 │ 3628800 │
╰────┴─────────╯
</pre>
 
=={{header|Nyquist}}==
Line 7,116 ⟶ 7,315:
(* n (factorial (1- n)))))</syntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<syntaxhighlight lang="oberon2modula2">
MODULE Factorial;
IMPORT
Line 7,183 ⟶ 7,382:
Recursive 8! =40320
Recursive 9! =362880
</pre>
 
=={{header|Oberon-07}}==
Almost identical to the Oberon-2 sample, with minor output formatting differences.<br/>
Oberon-2 allows single or double quotes to delimit strings whereas Oberon-07 only allows double quotes. Also, the LONGINT type does not exist in Oberon-07 (though some compilers may accept is as a synonym for INTEGER).
<syntaxhighlight lang="modula2">
MODULE Factorial;
IMPORT
Out;
 
VAR
i: INTEGER;
 
PROCEDURE Iterative(n: INTEGER): INTEGER;
VAR
i, r: INTEGER;
BEGIN
ASSERT(n >= 0);
r := 1;
FOR i := n TO 2 BY -1 DO
r := r * i
END;
RETURN r
END Iterative;
 
PROCEDURE Recursive(n: INTEGER): INTEGER;
VAR
r: INTEGER;
BEGIN
ASSERT(n >= 0);
r := 1;
IF n > 1 THEN
r := n * Recursive(n - 1)
END;
RETURN r
END Recursive;
 
BEGIN
FOR i := 0 TO 9 DO
Out.String("Iterative ");Out.Int(i,0);Out.String("! =");Out.Int(Iterative(i),8);Out.Ln;
END;
Out.Ln;
FOR i := 0 TO 9 DO
Out.String("Recursive ");Out.Int(i,0);Out.String("! =");Out.Int(Recursive(i),8);Out.Ln;
END
END Factorial.
</syntaxhighlight>
{{out}}
<pre>
Iterative 0! = 1
Iterative 1! = 1
Iterative 2! = 2
Iterative 3! = 6
Iterative 4! = 24
Iterative 5! = 120
Iterative 6! = 720
Iterative 7! = 5040
Iterative 8! = 40320
Iterative 9! = 362880
 
Recursive 0! = 1
Recursive 1! = 1
Recursive 2! = 2
Recursive 3! = 6
Recursive 4! = 24
Recursive 5! = 120
Recursive 6! = 720
Recursive 7! = 5040
Recursive 8! = 40320
Recursive 9! = 362880
</pre>
 
Line 8,646 ⟶ 8,915:
Memoized:
<syntaxhighlight lang="red">fac: function [n][m: #(0 1) any [m/:n m/:n: n * fac n - 1]]</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Facts 0 10>;
}
 
Facts {
s.N s.Max, <Compare s.N s.Max>: '+' = ;
s.N s.Max = <Prout <Symb s.N>'! = ' <Fact s.N>>
<Facts <+ s.N 1> s.Max>;
};
 
Fact {
0 = 1;
s.N = <* s.N <Fact <- s.N 1>>>;
};</syntaxhighlight>
{{out}}
<pre>0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800</pre>
 
=={{header|Relation}}==
Line 9,093 ⟶ 9,390:
===Imperative===
An imperative style using a mutable variable:
<syntaxhighlight lang="scala">def factorial(n: Int)={
def factorial(n: Int) = {
var res = 1
for (i <- 1 to n)
res *= i
res
}
}</syntaxhighlight>
</syntaxhighlight>
 
===Recursive===
Using naive recursion:
<syntaxhighlight lang="scala">def factorial(n: Int): Int =
def if factorial(n: == 0Int): 1Int =
if (n < 1) 1
else n * factorial(n-1)</syntaxhighlight>
else n * factorial(n - 1)
</syntaxhighlight>
 
Using tail recursion with a helper function:
<syntaxhighlight lang="scala">def factorial(n: Int) = {
def factorial(n: Int) = {
@tailrec def fact(x: Int, acc: Int): Int = {
if (x < 2) acc else fact(x - 1, acc * x)
}
fact(n, 1)
}
}</syntaxhighlight>
</syntaxhighlight>
 
===Stdlib .product===
Line 9,122 ⟶ 9,425:
===Folding===
Using folding:
<syntaxhighlight lang="scala">def factorial(n: Int) =
def factorial(n: Int) =
(2 to n).foldLeft(1)(_ * _)</syntaxhighlight>
(2 to n).foldLeft(1)(_ * _)
</syntaxhighlight>
 
===Using implicit functions to extend the Int type===
Enriching the integer type to support unary exclamation mark operator and implicit conversion to big integer:
<syntaxhighlight lang="scala">implicit def IntToFac(i : Int) = new {
implicit def IntToFac(i : Int) = new {
def ! = (2 to i).foldLeft(BigInt(1))(_ * _)
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out | Example used in the REPL}}
Line 9,142 ⟶ 9,449:
=={{header|Scheme}}==
===Recursive===
<syntaxhighlight lang="scheme">(define (factorial n)
(define (factorial n)
(if (<= n 0)
1
(* n (factorial (- n 1)))))</syntaxhighlight>
</syntaxhighlight>
The following is tail-recursive, so it is effectively iterative:
<syntaxhighlight lang="scheme">(define (factorial n)
(define (factorial n)
(let loop ((i 1)
(accum 1))
(if (> i n)
accum
(loop (+ i 1) (* accum i)))))</syntaxhighlight>
</syntaxhighlight>
 
===Iterative===
<syntaxhighlight lang="scheme">(define (factorial n)
(define (factorial n)
(do ((i 1 (+ i 1))
(accum 1 (* accum i)))
((> i n) accum)))</syntaxhighlight>
</syntaxhighlight>
 
===Folding===
<syntaxhighlight lang="scheme">;Using a generator and a function that apply generated values to a function taking two arguments
;Using a generator and a function that apply generated values to a function taking two arguments
 
;A generator knows commands 'next? and 'next
Line 9,181 ⟶ 9,497:
 
(factorial 8)
;40320</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Scilab}}==
Line 9,503 ⟶ 9,820:
3628800 3628800 3628800
39916800 39916800 39916800</pre>
 
=={{header|Soda}}==
 
===Recursive===
<syntaxhighlight lang="soda">
factorial (n : Int) : Int =
if n < 2
then 1
else n * factorial (n - 1)
</syntaxhighlight>
 
===Tail recursive===
<syntaxhighlight lang="soda">
_tailrec_fact (n : Int) (accum : Int) : Int =
if n < 2
then accum
else _tailrec_fact (n - 1) (n * accum)
 
factorial (n : Int) : Int =
_tailrec_fact (n) (1)
</syntaxhighlight>
 
=={{header|SparForte}}==
Line 9,781 ⟶ 10,119:
expr {round([::math::special::Gamma [expr {$n+1}]])}
}</syntaxhighlight>
 
=={{header|TI-57}}==
The program stack has only three levels, which means that the recursive approach can be dispensed with.
{| class="wikitable"
! Machine code
! Comment
|-
|
Lbl 0
C.t
x=t
1
STO 0
Lbl 1
RCL 0
×
Dsz
GTO 1
1
=
R/S
RST
|
program factorial(x) // x is the display register
if x=0 then
x=1
r0 = x
loop
multiply r0 by what will be in the next loop
decrement r0 and exit loop if r0 = 0
end loop
complete the multiplication sequence
return x!
end program
reset program pointer
|}
 
=={{header|TorqueScript}}==
Line 9,955 ⟶ 10,331:
{{out}}
<pre><1,1,2,6,24,120,720,5040,40320></pre>
 
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">@factorial ( n* -: fact* )
ORAk ?{ POP2 #0001 JMP2r }
DUP2 #0001 SUB2 factorial MUL2
JMP2r</syntaxhighlight>
 
=={{header|Verbexx}}==
Line 10,431 ⟶ 10,813:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./big" for BigInt
 
class Factorial {
Line 10,536 ⟶ 10,918:
int N; \range: 0..12
return if N<2 then 1 else N*FactRecur(N-1);</syntaxhighlight>
 
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
#!/usr/bin/env ys-0
 
defn main(n):
say: "$n! = $factorial(n)"
 
defn factorial(x):
apply *: 2 .. x
</syntaxhighlight>
 
=={{header|Zig}}==
{{Works with|Zig|0.11.0}}
Supports all integer data types, and checks for both overflow and negative numbers; returns null when there is a domain error.
<syntaxhighlight lang="zig">
const stdout = @import("std").io.getStdOut().outStream();
 
pub fn factorial(comptime Num: type, n: i8) ?Num {
return if (@typeInfo(Num) != .Int)
@compileError("factorial called with numnon-integral type: " ++ @typeName(Num))
else if (n < 0)
null
Line 10,551 ⟶ 10,943:
var fac: Num = 1;
while (i <= n) : (i += 1) {
ifconst tmp = (@mulWithOverflow(Num, fac, i, &fac));
if (tmp[1] != break :calc null;0)
break :calc null; // overflow
fac = tmp[0];
} else break :calc fac;
};
Line 10,558 ⟶ 10,952:
 
pub fn main() !void {
tryconst stdout.print("-1! = {}\n@import(", std").{factorialio.getStdOut(i32, -1)}.writer();
 
try stdout.print("0! = {}\n", .{factorial(i32, 0)});
try stdout.print("5-1! = {?}\n", .{factorial(i32, 5-1)});
try stdout.print("330!(64 bit) = {?}\n", .{factorial(i64i32, 330)}); // not vailid i64 factorial
try stdout.print("335! = {?}\n", .{factorial(i128i32, 335)}); // biggest facorial possible
try stdout.print("3433!(64 bit) = {?}\n", .{factorial(i128i64, 3433)}); // willnot overflowvalid i64 factorial
try stdout.print("33! = {?}\n", .{factorial(i128, 33)}); // biggest i128 factorial possible
try stdout.print("34! = {?}\n", .{factorial(i128, 34)}); // will overflow
}
</syntaxhighlight>
890

edits