Factorial: Difference between revisions

Add SmallBASIC
(Add SmallBASIC)
 
(6 intermediate revisions by 3 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
writeln .factorial(7)</syntaxhighlight>
 
==={{header|TI-83 BASIC}}===
Line 6,041 ⟶ 6,052:
=={{header|langur}}==
=== Folding ===
<syntaxhighlight lang="langur">val .factorial = fn(.n) fold(fn{*}, 2 .. .n)
val factorial = fn n: fold(fn{*}, 2 .. n)
writeln .factorial(7)</syntaxhighlight>
writeln .factorial(7)</syntaxhighlight>
</syntaxhighlight>
 
=== Recursive ===
<syntaxhighlight lang="langur">val .factorial = fn(.x) { if(.x < 2: 1; .x * self(.x - 1)) }
writelnval .factorial = fn x: if(7)x </syntaxhighlight> 2: 1; x * fn((x - 1)))
writeln factorial(7)
</syntaxhighlight>
 
=== Iterative ===
<syntaxhighlight lang="langur">val .factorial = fn(.i) {
val factorial = fn(i) {
var .answer = 1
forvar .xanswer in= 2 .. .i {1
for x in 2 .answer. *=i .x{
var . answer *= 1x
}
.answer
}
 
writeln .factorial(7)</syntaxhighlight>
</syntaxhighlight>
 
=== Iterative Folding ===
<syntaxhighlight lang="langur">val .factorial = fn(.n) { for[=1] .x in .n { _for *= .x } }
val factorial = fn n: for[=1] x in n { _for *= x }
writeln .factorial(7)</syntaxhighlight>
writeln factorial(7)
</syntaxhighlight>
 
{{out}}
Line 9,385 ⟶ 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}}==
Line 10,295 ⟶ 10,366:
echo $f
# => 479001600</syntaxhighlight>
 
=={{header|Uiua}}==
<syntaxhighlight lang="uiua">Factorial = /×+1⇡</syntaxhighlight>
 
=={{header|Ursa}}==
25

edits