update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
|
|
@ -1,15 +1,15 @@
|
|||
import std.stdio, std.algorithm, std.metastrings, std.range;
|
||||
|
||||
// iterative
|
||||
int factorial(int n) {
|
||||
int result = 1;
|
||||
long factorial(long n) {
|
||||
long result = 1;
|
||||
foreach (i; 1 .. n + 1)
|
||||
result *= i;
|
||||
return result;
|
||||
}
|
||||
|
||||
// recursive
|
||||
int recFactorial(int n) {
|
||||
long recFactorial(long n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
else
|
||||
|
|
@ -17,13 +17,13 @@ int recFactorial(int n) {
|
|||
}
|
||||
|
||||
// functional-style
|
||||
int fact(int n) {
|
||||
long fact(long n) {
|
||||
return iota(1, n + 1).reduce!q{a * b}();
|
||||
}
|
||||
|
||||
// tail recursive (at run-time, with DMD)
|
||||
int tfactorial(int n) {
|
||||
static int facAux(int n, int acc) {
|
||||
long tfactorial(long n) {
|
||||
static long facAux(long n, long acc) {
|
||||
if (n < 1)
|
||||
return acc;
|
||||
else
|
||||
|
|
|
|||
49
Task/Factorial/Eiffel/factorial.e
Normal file
49
Task/Factorial/Eiffel/factorial.e
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
note
|
||||
description: "recursive and iterative factorial example of a positive integer."
|
||||
|
||||
class
|
||||
FACTORIAL_EXAMPLE
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature -- Initialization
|
||||
|
||||
make
|
||||
local
|
||||
n: NATURAL
|
||||
do
|
||||
n := 5
|
||||
print ("%NFactorial of " + n.out + " = ")
|
||||
print (recursive_factorial (n))
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
recursive_factorial (n: NATURAL): NATURAL
|
||||
-- factorial of 'n'
|
||||
do
|
||||
if n = 0 then
|
||||
Result := 1
|
||||
else
|
||||
Result := n * recursive_factorial (n - 1)
|
||||
end
|
||||
end
|
||||
|
||||
iterative_factorial (n: NATURAL): NATURAL
|
||||
-- factorial of 'n'
|
||||
local
|
||||
v: like n
|
||||
do
|
||||
from
|
||||
Result := 1
|
||||
v := n
|
||||
until
|
||||
v <= 1
|
||||
loop
|
||||
Result := Result * v
|
||||
v := v - 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
# this is just a copy from the combinatorics.jl module of the Julia standard library
|
||||
function factorial(n::Integer)
|
||||
if n < 0
|
||||
return zero(n)
|
||||
|
|
@ -9,8 +8,3 @@ function factorial(n::Integer)
|
|||
end
|
||||
return f
|
||||
end
|
||||
|
||||
load("bigint.jl") #use BigInt to avoid overflow for large values of n
|
||||
n=BigInt(30)
|
||||
factorial(n)
|
||||
265252859812191058636308480000000
|
||||
|
|
|
|||
19
Task/Factorial/Run-BASIC/factorial.run
Normal file
19
Task/Factorial/Run-BASIC/factorial.run
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
for i = 0 to 100
|
||||
print " fctrI(";right$("00";str$(i),2); ") = "; fctrI(i)
|
||||
print " fctrR(";right$("00";str$(i),2); ") = "; fctrR(i)
|
||||
next i
|
||||
end
|
||||
|
||||
function fctrI(n)
|
||||
fctrI = 1
|
||||
if n >1 then
|
||||
for i = 2 To n
|
||||
fctrI = fctrI * i
|
||||
next i
|
||||
end if
|
||||
end function
|
||||
|
||||
function fctrR(n)
|
||||
fctrR = 1
|
||||
if n > 1 then fctrR = n * fctrR(n -1)
|
||||
end function
|
||||
Loading…
Add table
Add a link
Reference in a new issue