Created
May 1, 2013 18:55
-
-
Save ice09/5497460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Write a Fibonacci program... | |
# | |
Proto := Object clone | |
# ...recursive version | |
Proto fib := method(n, if (n<=2, return 1, return (fib(n-2)+fib(n-1)))) | |
# ...iterative version | |
Proto fib := method(n, | |
numbers := list(1,1) | |
if (n <= 2, | |
return numbers at(n-1), | |
for(i, 2, n, numbers := numbers append (numbers at(i-2) + numbers at(i-1))) | |
numbers at(n-1) | |
) | |
) | |
Proto fib(4) | |
==> 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment