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 04:31
This graph compares the cost of room and board at educational institutions in texas.
Answers: 1
question
Computers and Technology, 23.06.2019 15:00
Barbara is interested in pursuing a career in the science and math pathway. which qualifications will her reach that goal? a.an advanced knowledge of physics and math b.an advanced knowledge of engineering and math c. an advanced knowledge of physics and robotics an d. advanced knowledge of machinery and math
Answers: 2
question
Computers and Technology, 23.06.2019 19:30
What are loans to a company or government for a set amount of time
Answers: 1
question
Computers and Technology, 23.06.2019 20:10
Leo is a recruitment executive for a large company. he has identified new labor resource requirements in both the marketing and production departments. what should be his first step in recruiting candidates for the positions? a. conduct background checks of candidates b. make job offers c. arrange interviews d. conduct reference checks e. place job ads on job sites
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
Physics, 11.12.2021 09:10
question
Social Studies, 11.12.2021 09:10
Questions on the website: 13722359