This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,7 @@
100 N = 4 : GOSUB 200"FACTORIAL
110 PRINT N
120 END
200 N = INT(N)
210 IF N > 1 THEN FOR I = N - 1 TO 2 STEP -1 : N = N * I : NEXT I
220 RETURN

View file

@ -0,0 +1,9 @@
10 A = 768:L = 7
20 DATA 165,157,240,3
30 DATA 32,149,217,96
40 FOR I = A TO A + L
50 READ B: POKE I,B: NEXT
60 H = 256: POKE 12,A / H
70 POKE 11,A - PEEK (12) * H
80 DEF FN FA(N) = USR (N < 2) + N * FN FA(N - 1)
90 PRINT FN FA(4)

View file

@ -0,0 +1,12 @@
print "enter a number, n = ";
input n
print string(n) + "! = " + string(factorial(n))
function factorial(n)
factorial = 1
if n > 0 then
for p = 1 to n
factorial *= p
next p
end if
end function

View file

@ -0,0 +1,11 @@
print "enter a number, n = ";
input n
print string(n) + "! = " + string(factorial(n))
function factorial(n)
if n > 0 then
factorial = n * factorial(n-1)
else
factorial = 1
end if
end function

View file

@ -0,0 +1 @@
MOVE FUNCTION FACTORIAL(num) TO result

View file

@ -0,0 +1,20 @@
IDENTIFICATION DIVISION.
FUNCTION-ID. factorial.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 i PIC 9(10).
LINKAGE SECTION.
01 n PIC 9(10).
01 ret PIC 9(10).
PROCEDURE DIVISION USING BY VALUE n RETURNING ret.
MOVE 1 TO ret
PERFORM VARYING i FROM 2 BY 1 UNTIL n < i
MULTIPLY i BY ret
END-PERFORM
GOBACK
.

View file

@ -0,0 +1,21 @@
IDENTIFICATION DIVISION.
FUNCTION-ID. factorial.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 prev-n PIC 9(10).
LINKAGE SECTION.
01 n PIC 9(10).
01 ret PIC 9(10).
PROCEDURE DIVISION USING BY VALUE n RETURNING ret.
IF n = 0
MOVE 1 TO ret
ELSE
SUBTRACT 1 FROM n GIVING prev-n
MULTIPLY n BY fac(prev-n) GIVING ret
END-IF
GOBACK
.

View file

@ -0,0 +1,7 @@
proc fac(n) {
var r = 1;
for i in 1..n do
r *= i;
return r;
}

View file

@ -1,29 +1,29 @@
import std.stdio, std.algorithm, std.metastrings, std.range;
import std.stdio, std.algorithm, std.range;
// iterative
long factorial(long n) {
long result = 1;
/// Iterative.
int factorial(in int n) {
int result = 1;
foreach (i; 1 .. n + 1)
result *= i;
return result;
}
// recursive
long recFactorial(long n) {
/// Recursive.
int recFactorial(in int n) {
if (n == 0)
return 1;
else
return n * recFactorial(n - 1);
}
// functional-style
long fact(long n) {
return iota(1, n + 1).reduce!q{a * b}();
/// Functional-style.
int fact(in int n) {
return iota(1, n + 1).reduce!q{a * b};
}
// tail recursive (at run-time, with DMD)
long tfactorial(long n) {
static long facAux(long n, long acc) {
/// Tail recursive (at run-time, with DMD).
int tfactorial(in int n) {
static int facAux(int n, int acc) {
if (n < 1)
return acc;
else
@ -32,16 +32,16 @@ long tfactorial(long n) {
return facAux(n, 1);
}
// computed and printed at compile-time
pragma(msg, toStringNow!(factorial(15)));
pragma(msg, toStringNow!(recFactorial(15)));
pragma(msg, toStringNow!(fact(15)));
pragma(msg, toStringNow!(tfactorial(15)));
// Computed and printed at compile-time.
pragma(msg, 15.factorial);
pragma(msg, 15.recFactorial);
pragma(msg, 15.fact);
pragma(msg, 15.tfactorial);
void main() {
// computed and printed at run-time
writeln(factorial(15));
writeln(recFactorial(15));
writeln(fact(15));
writeln(tfactorial(15));
// Computed and printed at run-time.
15.factorial.writeln;
15.recFactorial.writeln;
15.fact.writeln;
15.tfactorial.writeln;
}

View file

@ -0,0 +1,9 @@
julia> help(factorial)
Loading help data...
Base.factorial(n)
Factorial of n
Base.factorial(n, k)
Compute "factorial(n)/factorial(k)"

View file

@ -0,0 +1,10 @@
function factorial(n::Integer)
if n < 0
return zero(n)
end
f = one(n)
for i = 2:n
f *= i
end
return f
end

View file

@ -0,0 +1 @@
fact(n) = prod(1:big(n))

View file

@ -0,0 +1,2 @@
sub postfix:<!>($n) { [*] 2..$n }
say 5!;

View file

@ -0,0 +1,2 @@
constant fact = 1, [\*] 1..*;
say fact[5]