fib(n)
, where n
is a number we pass the
function, telling it how many Fibonacci numbers we want it
to print. So we'd call it by programming fib(10)
to see the first
10 Fibonacci numbers (past the initial 0 and 1, etc.).
first =
, second =
and next =
lines according to the description of the
Fibonacci sequence described above.
function fib(n)
first = ??
second = ??
print(first,second)
for i=0, n do
next = ???
first = second
second = next
print(next)
end
end
print(fib(10))
first =
and second =
lines? After this, the next
Fibonacci number is 3, which is the sum of first
and second
. So, what should you put
in for the next =
line?
Your code is working with it displays this sequence of numbers: 0, 1, 2, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...
Not the logic: the old second number becomes the new first number. And the current number becomes the new second number, so the next time through the loop, the next number can be found buy adding the first and second once again.