March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,19 @@
uint factorial(in uint n) pure nothrow
in {
assert(n <= 12);
} body {
uint result = 1;
foreach (immutable i; 1 .. n + 1)
result *= i;
return result;
}
// Computed and printed at compile-time.
pragma(msg, 12.factorial);
void main() {
import std.stdio;
// Computed and printed at run-time.
12.factorial.writeln;
}

View file

@ -0,0 +1,19 @@
uint factorial(in uint n) pure nothrow
in {
assert(n <= 12);
} body {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
// Computed and printed at compile-time.
pragma(msg, 12.factorial);
void main() {
import std.stdio;
// Computed and printed at run-time.
12.factorial.writeln;
}

View file

@ -0,0 +1,16 @@
import std.stdio, std.algorithm, std.range;
uint factorial(in uint n) pure nothrow
in {
assert(n <= 12);
} body {
return reduce!q{a * b}(1u, iota(1, n + 1));
}
// Computed and printed at compile-time.
pragma(msg, 12.factorial);
void main() {
// Computed and printed at run-time.
12.factorial.writeln;
}

View file

@ -0,0 +1,22 @@
uint factorial(in uint n) pure nothrow
in {
assert(n <= 12);
} body {
static uint inner(uint n, uint acc) pure nothrow {
if (n < 1)
return acc;
else
return inner(n - 1, acc * n);
}
return inner(n, 1);
}
// Computed and printed at compile-time.
pragma(msg, 12.factorial);
void main() {
import std.stdio;
// Computed and printed at run-time.
12.factorial.writeln;
}

View file

@ -1,47 +0,0 @@
import std.stdio, std.algorithm, std.range;
/// Iterative.
int factorial(in int n) {
int result = 1;
foreach (i; 1 .. n + 1)
result *= i;
return result;
}
/// Recursive.
int recFactorial(in int n) {
if (n == 0)
return 1;
else
return n * recFactorial(n - 1);
}
/// Functional-style.
int fact(in int n) {
return iota(1, n + 1).reduce!q{a * b};
}
/// 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
return facAux(n - 1, acc * n);
}
return facAux(n, 1);
}
// 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.
15.factorial.writeln;
15.recFactorial.writeln;
15.fact.writeln;
15.tfactorial.writeln;
}

View file

@ -0,0 +1 @@
factorial[x] := x!

View file

@ -0,0 +1 @@
factorial2[x] := product[1 to x]

View file

@ -0,0 +1,19 @@
fn factorial_recursive (n: uint) -> uint {
match n {
0 => 1,
_ => n * factorial_recursive(n-1)
}
}
fn factorial_iterative(n: uint) -> uint {
range(1u, n+1).fold(1, |p, t| p * t)
}
fn main () {
for i in range(1u, 10u) {
println!("{}", factorial_recursive(i))
}
for i in range(1u, 10u) {
println!("{}", factorial_iterative(i))
}
}

View file

@ -0,0 +1,2 @@
$ txr -p '(n-perm-k 10 10)'
3628800

View file

@ -0,0 +1,2 @@
$ txr -p '[reduce-left * (range 1 10) 1]'
3628800