Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Apr 24, 2014

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

@mvidaurre

This comment has been minimized.

Copy link
Owner Author

commented Apr 26, 2014

Integers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.