Skip to content

Instantly share code, notes, and snippets.

@mvidaurre
Created April 17, 2014 19:30
Show Gist options
  • Save mvidaurre/11006570 to your computer and use it in GitHub Desktop.
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
# 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
@djvita
Copy link

djvita commented Apr 24, 2014

im used to strong typed langs like java, what value types are the parameters and return values?

@mvidaurre
Copy link
Author

Integers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment