Factorial: Difference between revisions
Content added Content deleted
No edit summary |
mNo edit summary |
||
Line 4,655: | Line 4,655: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
window 1, @"Factorial", ( 0, 0, 300, 550 ) |
|||
local fn factorialIterative( n as long ) as double |
|||
double f |
|||
long i |
|||
if ( n > 1 ) |
|||
f = 1 |
|||
for i = 2 to n |
|||
f = f * i |
|||
next |
|||
else |
|||
f = 1 |
|||
end if |
|||
end fn = f |
|||
local fn factorialRecursive( n as long ) as double |
|||
double f |
|||
if ( n < 2 ) |
|||
f = 1 |
|||
else |
|||
f = n * fn factorialRecursive( n -1 ) |
|||
end if |
|||
end fn = f |
|||
long i |
|||
for i = 0 to 12 |
|||
print "Iterative:"; using "####"; i; " = "; fn factorialIterative( i ) |
|||
print "Recursive:"; using "####"; i; " = "; fn factorialRecursive( i ) |
|||
print |
|||
next |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
Iterative: 0 = 1 |
|||
Recursive: 0 = 1 |
|||
Iterative: 1 = 1 |
|||
Recursive: 1 = 1 |
|||
Iterative: 2 = 2 |
|||
Recursive: 2 = 2 |
|||
Iterative: 3 = 6 |
|||
Recursive: 3 = 6 |
|||
Iterative: 4 = 24 |
|||
Recursive: 4 = 24 |
|||
Iterative: 5 = 120 |
|||
Recursive: 5 = 120 |
|||
Iterative: 6 = 720 |
|||
Recursive: 6 = 720 |
|||
Iterative: 7 = 5040 |
|||
Recursive: 7 = 5040 |
|||
Iterative: 8 = 40320 |
|||
Recursive: 8 = 40320 |
|||
Iterative: 9 = 362880 |
|||
Recursive: 9 = 362880 |
|||
Iterative: 10 = 3628800 |
|||
Recursive: 10 = 3628800 |
|||
Iterative: 11 = 39916800 |
|||
Recursive: 11 = 39916800 |
|||
Iterative: 12 = 479001600 |
|||
Recursive: 12 = 479001600 |
|||
</pre> |
|||