Created
April 17, 2014 19:30
-
-
Save mvidaurre/11006570 to your computer and use it in GitHub Desktop.
Calculate the nth Fibonacci number, f(n). Using invariants for https://github.com/RayHightower/fibonacci
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
# Calculate the nth Fibonacci number, f(n). Using invariants | |
def fibo_tr(n, acc1, acc2) | |
if n == 0 | |
0 | |
elsif n < 2 | |
acc2 | |
else | |
return fibo_tr(n - 1, acc2, acc2 + acc1) | |
end | |
end | |
def fibo (n) | |
fibo_tr(n, 0, 1) | |
end | |
# Display the Fibonacci sequence. | |
(1..40).each do |number| | |
puts "fibo(#{number}) = #{fibo(number)}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
im used to strong typed langs like java, what value types are the parameters and return values?