Factorial: Difference between revisions

1,303 bytes added ,  1 month ago
m
fixed incorrect placement
(Added Oberon-07, use modula2 for syntax highlighting Oberon-2 and Oberon-07)
m (fixed incorrect placement)
(4 intermediate revisions by 3 users not shown)
Line 6,045:
 
=== Recursive ===
<syntaxhighlight lang="langur">val .factorial = fn(.x) { if(.x < 2: 1; .x * self(.x - 1)) }
writeln .factorial(7)</syntaxhighlight>
 
Line 6,060:
 
=== Iterative Folding ===
<syntaxhighlight lang="langur">val .factorial = fn(.n) { for[=1] .x in .n { _for *= .x } }
writeln .factorial(7)</syntaxhighlight>
 
Line 9,385:
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 10,295 ⟶ 10,347:
echo $f
# => 479001600</syntaxhighlight>
 
=={{header|Uiua}}==
<syntaxhighlight lang="uiua">Factorial = /×+1⇡</syntaxhighlight>
 
=={{header|Ursa}}==
19

edits