Fibonacci (optional)
This is an optional assignment and can be a little tricky. The Fibonacci sequence is an infinite sequence of numbers that starts with 0 and 1 and then adds the previous number with the current to get the next number.
round 1:
0 <-- previous
1 <-- current
1 <-- new
round 2:
0
1 <-- previous
1 <-- current
2 <-- new
This sequence has many real-world applications and is even a naturally occurring phenomenon in plants and animals. This assignment will not introduce you to any real-world applications but is a fun puzzle to solve that will allow you to flex your new loop-building muscles.
Setup
Fork the following repository and clone your copy (if you haven't already for this lesson).
Open fibonacci.py
in your text editor.
Requirements
General
ask the user if they want to:
1) calculate the nth Fibonacci number 2) calculate the last Fibonacci number less than or equal to n 3) quit
Loops
solve the selected option using loops. Each option lends itself more to a
certain loop type. It is your job to find out which one goes with which
Tips
the first 10 Fibonacci numbers are:
0 1 1 2 3 5 8 13 21 34
an efficient algorithm will require you to swap the values of two variables.
You can do this using a temporary variable. Since this is a bit tricky, I will
provide you with the part you will need.
temp = current current += previous previous = temp
Last updated
Was this helpful?