Practice · Lesson 27 of 27
Practice: Fibonacci
In the Fibonacci series, each number is the sum of the two before it:
0 1 1 2 3 5 8 13 21 ...
Keep two variables for the last two numbers and move them forward each time:
a, b = 0, 1 a, b = b, a + b # now a = 1, b = 1 a, b = b, a + b # now a = 1, b = 2
a, b = b, a + b updates both at the same time, so you don't need a temporary variable.
Your task
Read n and print the first n Fibonacci numbers on one line, separated by spaces.
For n = 7: 0 1 1 2 3 5 8