March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
19
Task/Factorial/D/factorial-1.d
Normal file
19
Task/Factorial/D/factorial-1.d
Normal 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;
|
||||
}
|
||||
19
Task/Factorial/D/factorial-2.d
Normal file
19
Task/Factorial/D/factorial-2.d
Normal 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;
|
||||
}
|
||||
16
Task/Factorial/D/factorial-3.d
Normal file
16
Task/Factorial/D/factorial-3.d
Normal 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;
|
||||
}
|
||||
22
Task/Factorial/D/factorial-4.d
Normal file
22
Task/Factorial/D/factorial-4.d
Normal 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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue