Jump to content

Factorial: Difference between revisions

2,156 bytes added ,  25 days ago
m (scheme syntax highlight)
(10 intermediate revisions by 8 users not shown)
Line 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,972 ⟶ 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,368 ⟶ 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,449 ⟶ 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,028 ⟶ 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,306 ⟶ 7,315:
(* n (factorial (1- n)))))</syntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<syntaxhighlight lang="oberon2modula2">
MODULE Factorial;
IMPORT
Line 7,373 ⟶ 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 10,842 ⟶ 10,921:
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
#!/usr/bin/env ys-0
!yamlscript/v0
 
defn factorialmain(xn):
say: "$n! = $factorial(n)"
apply *: 2..x
 
say:defn factorial(20x):
apply *: 2 .. x
</syntaxhighlight>
 
885

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.