2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
// -*- rust -*-
|
2011-07-27 07:19:39 -05:00
|
|
|
fn fib(n: int) -> int {
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
// Several of the posted 'benchmark' versions of this compute the
|
|
|
|
// wrong Fibonacci numbers, of course.
|
2011-07-27 07:19:39 -05:00
|
|
|
if n == 0 {
|
2011-06-15 13:19:50 -05:00
|
|
|
ret 0;
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { if n <= 2 { ret 1; } else { ret fib(n - 1) + fib(n - 2); } }
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (fib(8) == 21);
|
|
|
|
assert (fib(15) == 610);
|
|
|
|
log fib(8);
|
|
|
|
log fib(15);
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|