subject

I'm working on an assignment for my class and the prompt is: The first and second numbers in the Fibonacci sequence are both 1. After that, each subsequent number is the sum of the two preceding numbers. The first several numbers in the sequence are: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, etc. Write a function named fib that takes a positive integer parameter and returns the number at that position of the Fibonacci sequence. For example fib(1) = 1, fib(3) = 2, fib(10) = 55, etc. Your function does not need to print anything out - just return a value.

You cannot use recursion, since we haven't covered that technique. That means that for this assignment you cannot have your function call itself. This includes indirect recursion, such as one function calling a second function that calls the first function. You must use a loop to step through the sequence - do not use the golden ratio to directly calculate the value.

What I have for the code is:

def fib(nterms):
n1, n2 = 1, 1
count = 0
while count < nterms:
term = n1
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
return term

I'm not sure if I'm doing this correctly with no recursion and was seeing if this was close.

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 00:30
Knowing that the central portion of link bd has a uniform cross sectional area of 800 mm2 , determine the magnitude of the load p for which the normal stress in link bd is 50 mpa. (hint: link bd is a two-force member.) ans: p = 62.7 kn
Answers: 2
question
Computers and Technology, 23.06.2019 01:30
For a typical middle-income family, what is the estimated cost of raising a child to the age of 18? $145,500 $245,340 $304,340 $455,500
Answers: 2
question
Computers and Technology, 23.06.2019 03:30
In vista and windows 7, the appearance and personalization option allows you to change the
Answers: 1
question
Computers and Technology, 23.06.2019 03:50
Iam a bacterium. i cause stomach cramps and diarrhea. i am caused by eating rotten foodssuch as chicken, fish, or eggs. sometimes turtles carry my bacteria.what am i?
Answers: 2
You know the right answer?
I'm working on an assignment for my class and the prompt is: The first and second numbers in the Fi...
Questions
question
Engineering, 04.09.2020 08:01
Questions on the website: 13722367