37 lines
501 B
Plaintext
37 lines
501 B
Plaintext
import extensions;
|
|
|
|
fib(n)
|
|
{
|
|
if (n < 0)
|
|
{ InvalidArgumentException.raise() };
|
|
|
|
^ (n) {
|
|
if (n > 1)
|
|
{
|
|
^ this self(n - 2) + (this self(n - 1))
|
|
}
|
|
else
|
|
{
|
|
^ n
|
|
}
|
|
}(n)
|
|
}
|
|
|
|
public program()
|
|
{
|
|
for (int i := -1; i <= 10; i += 1)
|
|
{
|
|
Console.print("fib(",i,")=");
|
|
try
|
|
{
|
|
Console.printLine(fib(i))
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
Console.printLine("invalid")
|
|
}
|
|
};
|
|
|
|
Console.readChar()
|
|
}
|