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, 22.06.2019 18:00
What is the first view you place in your drawing?
Answers: 1
question
Computers and Technology, 23.06.2019 04:20
Which network media uses different regions of the electromagnetic spectrum to transmit signals through air? uses different regions of the electromagnetic spectrum to transmit signals through air.
Answers: 2
question
Computers and Technology, 24.06.2019 02:00
Which steps will open the system so that you can enter a question and do a search for
Answers: 1
question
Computers and Technology, 24.06.2019 06:30
Me and category do i put them in because this is science
Answers: 1
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
Mathematics, 15.03.2020 09:48
question
Engineering, 15.03.2020 09:50
question
English, 15.03.2020 09:50
question
Mathematics, 15.03.2020 10:02