Commit 81b59891 authored by Owen Ilagan's avatar Owen Ilagan

more revisions

parent fda54af5
def get_fibonacci(length)
# This function produces an array containing integers representing
# a Fibonacci sequence of a given length
result = []
length.times do |idx|
if idx == 0 # At the start of the loop just push the initial number
result.push(1)
elsif idx == 1 # At the second iteration just push another number
result.push(1)
else
a = result[idx-2] # Getting the last two numbers in the array
b = result[idx-1]
nextnum = a + b # Add the last two numbers of the array
result.push(nextnum)
end
end
return result
end
n = 10
puts get_fibonacci(n).join(',')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment