The Fibonacci sequence is a famous sequence in mathematics. The first element in the sequence is 0 and the second element is 1. The nth element is defined as Fn = Fn-1 + Fn-2- Implement the fib function, which takes an integer n and returns the n th Fibonacci number. Use a while loop in your solution. def fib(n): """Returns the nth Fibonacci number. >>> fib (0) 0 >>> fib(1) 1 >>> fib(2) 1 >>> fib(3) 2 >>> fib (4) 3 >>> fib(5) 5 >>> fib(6) 8 >>> fib (100) 354224848179261915075 www "**** YOUR CODE HERE ****"

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 20PE
icon
Related questions
Question
The Fibonacci sequence is a famous sequence in mathematics. The first element in the sequence is 0 and the second element
is 1. The nth element is defined as Fn = Fn-1 + Fn-2-
Implement the fib function, which takes an integer n and returns the n th Fibonacci number. Use a while loop in your
solution.
def fib(n):
"""Returns the nth Fibonacci number.
>>> fib (0)
0
>>> fib (1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5
>>> fib(6)
8
>>> fib (100)
354224848179261915075
"*** YOUR CODE HERE ***"
Transcribed Image Text:The Fibonacci sequence is a famous sequence in mathematics. The first element in the sequence is 0 and the second element is 1. The nth element is defined as Fn = Fn-1 + Fn-2- Implement the fib function, which takes an integer n and returns the n th Fibonacci number. Use a while loop in your solution. def fib(n): """Returns the nth Fibonacci number. >>> fib (0) 0 >>> fib (1) 1 >>> fib(2) 1 >>> fib(3) 2 >>> fib(4) 3 >>> fib(5) 5 >>> fib(6) 8 >>> fib (100) 354224848179261915075 "*** YOUR CODE HERE ***"
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT