Fibonacci numbers in D
Example for versions
D1,
D2,
gdc 0.24
This example uses iterative definition of Fibonacci numbers.
module fibonacci;
import std.stdio;
ulong iterative(ulong x)
{
ulong prev1 = 1L;
ulong prev2 = 1L;
ulong result = x <= 2 ? 1L : 0L;
for ( ulong i = 3; i <= x; ++i )
{
result = prev1 + prev2;
prev1 = prev2;
prev2 = result;
}
return result;
}
int main()
{
for (uint i = 1; i < 17; i++)
{
writef("%s, ", iterative(i));
}
writefln("%s", "...");
return 0;
}
Comments
]]>blog comments powered by Disqus
]]>