Skip to content

Instantly share code, notes, and snippets.

@ice09
Created May 1, 2013 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ice09/5497460 to your computer and use it in GitHub Desktop.
Save ice09/5497460 to your computer and use it in GitHub Desktop.
#
# 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