Factorial: Difference between revisions
Add SmallBASIC
(Added S-BASIC example) |
(Add SmallBASIC) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1,955:
{{out}}
<pre>6227020800</pre>
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
n = 10
factorial = 1
for ii = 2 to n
factorial = factorial * ii
next
print factorial
</syntaxhighlight>
==={{header|TI-83 BASIC}}===
Line 6,041 ⟶ 6,052:
=={{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
</syntaxhighlight>
=== Iterative Folding ===
<syntaxhighlight lang="langur">
val factorial = fn n: for[=1] x in n { _for *= x }
writeln factorial(7)
</syntaxhighlight>
{{out}}
Line 9,173 ⟶ 9,192:
Standard iterative solution using a <code>for</code> loop:
* <code>range(2, num, :incl)</code> creates an inclusive range (<code>2 <= i <= num</code>) for iteration
<syntaxhighlight lang="scala">
Line 9,436 ⟶ 9,404:
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}}==
|