added factorial
git-svn-id: svn://anubis/misc/d@92 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
d468e3e32c
commit
f1a82db38d
13
factorial/Makefile
Normal file
13
factorial/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
TARGET := factorial
|
||||||
|
DFLAGS := -funittest
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(TARGET).d
|
||||||
|
|
||||||
|
%: %.d
|
||||||
|
gdc -o $@ $(DFLAGS) $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm -f *.o *~ $(TARGET)
|
41
factorial/factorial.d
Normal file
41
factorial/factorial.d
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
uint factorial(int a)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* A factorial is a mathematical concept that
|
||||||
|
* is typically denoted with a "!".
|
||||||
|
* Example:
|
||||||
|
* 4! = 4 * 3 * 2 * 1 = 24
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(a == 0) return 1;
|
||||||
|
|
||||||
|
uint b = 1;
|
||||||
|
for(int i = 1; i <= a; i++)
|
||||||
|
b *= i;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
/* Compile with the "-unittest" option to run these unittests. */
|
||||||
|
|
||||||
|
writefln("Attempting unittests...");
|
||||||
|
assert(factorial(0) == 1);
|
||||||
|
assert(factorial(1) == 1);
|
||||||
|
assert(factorial(2) == 2);
|
||||||
|
assert(factorial(3) == 6);
|
||||||
|
assert(factorial(4) == 24);
|
||||||
|
writefln("unittests successful...");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
writefln("factorial(0): %d", factorial(0));
|
||||||
|
writefln("factorial(1): %d", factorial(1));
|
||||||
|
writefln("factorial(2): %d", factorial(2));
|
||||||
|
writefln("factorial(3): %d", factorial(3));
|
||||||
|
writefln("factorial(4): %d", factorial(4));
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user