Factorial: Difference between revisions
→{{header|langur}}
mNo edit summary |
Langurmonkey (talk | contribs) |
||
(34 intermediate revisions by 23 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 734 ⟶ 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,149 ⟶ 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,299 ⟶ 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,493 ⟶ 1,545:
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">'
print "version 1 without function"
for i = 1 to 12
let n = i
let f = 1
do
let f = f * n
let n = n - 1
loop n > 0
print f, " ",
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,918 ⟶ 1,985:
80 PRINT F
90 END</syntaxhighlight>
==={{header|True BASIC}}===
Line 2,314 ⟶ 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,395 ⟶ 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,731 ⟶ 3,798:
<syntaxhighlight lang="text">
r = 1
for i = 2 to n
r *= i
.
return r
.
</syntaxhighlight>
Line 5,236 ⟶ 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,954 ⟶ 6,041:
=={{header|langur}}==
=== Folding ===
<syntaxhighlight lang="langur">
val factorial = fn n: fold(fn{*}, 2 .. n)
writeln factorial(7)
</syntaxhighlight>
=== Recursive ===
<syntaxhighlight lang="langur">
writeln factorial(7)
</syntaxhighlight>
=== Iterative ===
<syntaxhighlight lang="langur">
val factorial = fn(i) {
for x in 2 .
answer *= x
}
}
writeln factorial(7)
</syntaxhighlight>
=== Iterative Folding ===
<syntaxhighlight lang="langur">
writeln
</syntaxhighlight>
{{out}}
Line 6,022 ⟶ 6,113:
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}}==
Line 7,165 ⟶ 7,285:
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,180 ⟶ 7,323:
(* n (factorial (1- n)))))</syntaxhighlight>
=={{header|Oberon-2}}==
{{works with|oo2c}}
<syntaxhighlight lang="
MODULE Factorial;
IMPORT
Line 7,247 ⟶ 7,390:
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,710 ⟶ 8,923:
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,152 ⟶ 9,393:
end;
end;</syntaxhighlight>
=={{header|S-BASIC}}==
S-BASIC's double-precision real data type supports up to 14 digits,
thereby allowing calculation up to 15! without loss of precision
<syntaxhighlight lang="BASIC">
function factorial(n=real.double)=real.double
if n = 0 then n = 1 else n = n * factorial(n-1)
end = n
var i=integer
print "Factorial Calculator"
print " n n!"
print "----------------------"
for i=1 to 15
print using "## #,###,###,###,###";i;factorial(i)
next i
end
</syntaxhighlight>
An iterative rather than recursive approach works equally well, if that
is your preference.
<syntaxhighlight lang="BASIC">
function factorial(n=real.double)=real.double
var i, f = real.double
f = 1
for i = 1 to n
f = f * i
next i
end = f
</syntaxhighlight>
{{out}}
<pre>
Factorial Calculator
n n!
----------------------
1 1
2 2
3 3
4 24
5 120
6 720
7 5,040
8 40,320
9 362,880
10 3,628,800
11 39,916,800
12 479,001,600
13 6,227,020,800
14 87,178,291,200
15 1,307,674,368,000
</pre>
=={{header|Scala}}==
Line 9,216 ⟶ 9,509:
=={{header|Scheme}}==
===Recursive===
<syntaxhighlight lang="scheme">
(define (factorial n)
(if (<= n 0)
1
(* n (factorial (- n 1)))))
</syntaxhighlight>
The following is tail-recursive, so it is effectively iterative:
<syntaxhighlight lang="scheme">
(define (factorial n)
(let loop ((i 1)
(accum 1))
(if (> i n)
accum
(loop (+ i 1) (* accum i)))))
</syntaxhighlight>
===Iterative===
<syntaxhighlight lang="scheme">
(define (factorial n)
(do ((i 1 (+ i 1))
(accum 1 (* accum i)))
((> i n) accum)))
</syntaxhighlight>
===Folding===
<syntaxhighlight lang="scheme">
;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,255 ⟶ 9,557:
(factorial 8)
;40320
</syntaxhighlight>
=={{header|Scilab}}==
Line 9,590 ⟶ 9,893:
===Tail recursive===
<syntaxhighlight lang="soda">
_tailrec_fact (n : Int) (accum : Int) : Int =
if n < 2
Line 10,053 ⟶ 10,355:
echo $f
# => 479001600</syntaxhighlight>
=={{header|Uiua}}==
<syntaxhighlight lang="uiua">Factorial = /×+1⇡</syntaxhighlight>
=={{header|Ursa}}==
Line 10,089 ⟶ 10,394:
{{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,565 ⟶ 10,876:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="
import "./big" for BigInt
class Factorial {
Line 10,670 ⟶ 10,981:
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">
pub fn factorial(comptime Num: type, n: i8) ?Num {
return if (@typeInfo(Num) != .Int)
@compileError("factorial called with
else if (n < 0)
null
Line 10,685 ⟶ 11,006:
var fac: Num = 1;
while (i <= n) : (i += 1) {
if (tmp[1] !=
break :calc null; // overflow
fac = tmp[0];
} else break :calc fac;
};
Line 10,692 ⟶ 11,015:
pub fn main() !void {
try stdout.print("
try stdout.print("
try stdout.print("
try stdout.print("
try stdout.print("33! = {?}\n", .{factorial(i128, 33)}); // biggest i128 factorial possible
try stdout.print("34! = {?}\n", .{factorial(i128, 34)}); // will overflow
}
</syntaxhighlight>
|