46 lines
878 B
Plaintext
46 lines
878 B
Plaintext
class FibonacciSequence
|
|
**Prints the nth fibonacci number**
|
|
|
|
on start
|
|
|
|
args := program arguments
|
|
|
|
if args empty
|
|
print .fibonacci(8)
|
|
|
|
else
|
|
|
|
try
|
|
print .fibonacci(integer.parse(args[0]))
|
|
|
|
catch FormatException
|
|
print to Console.error made !, "Input must be an integer"
|
|
exit program with error code
|
|
|
|
catch OverflowException
|
|
print to Console.error made !, "Number too large"
|
|
exit program with error code
|
|
|
|
define fibonacci(n as integer) as integer is shared
|
|
**Returns the nth fibonacci number**
|
|
|
|
test
|
|
assert fibonacci(0) = 0
|
|
assert fibonacci(1) = 1
|
|
assert fibonacci(2) = 1
|
|
assert fibonacci(3) = 2
|
|
assert fibonacci(4) = 3
|
|
assert fibonacci(5) = 5
|
|
assert fibonacci(6) = 8
|
|
assert fibonacci(7) = 13
|
|
assert fibonacci(8) = 21
|
|
|
|
|
|
body
|
|
a, b := 0, 1
|
|
|
|
for n
|
|
a, b := b, a + b
|
|
|
|
return a
|