Factorial: Difference between revisions

3,581 bytes added ,  17 days ago
(→‎Insitux: implementation)
(20 intermediate revisions by 16 users not shown)
Line 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,165 ⟶ 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,965 ⟶ 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,361 ⟶ 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,442 ⟶ 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 6,021 ⟶ 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 7,261 ⟶ 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,276 ⟶ 7,315:
(* n (factorial (1- n)))))</syntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<syntaxhighlight lang="oberon2modula2">
MODULE Factorial;
IMPORT
Line 7,343 ⟶ 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,806 ⟶ 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,312 ⟶ 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,351 ⟶ 9,497:
 
(factorial 8)
;40320</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Scilab}}==
Line 9,686 ⟶ 9,833:
===Tail recursive===
<syntaxhighlight lang="soda">
@tailrec
_tailrec_fact (n : Int) (accum : Int) : Int =
if n < 2
Line 10,185 ⟶ 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,661 ⟶ 10,813:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./big" for BigInt
 
class Factorial {
Line 10,769 ⟶ 10,921:
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
#!/usr/bin/env ys-0
defn factorial(x):
apply(*): (2 .. x)
 
defn main(n):
say: factorial(10)
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,789 ⟶ 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,796 ⟶ 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>
885

edits