Sep-30-2017, 11:53 PM
Here's the problem I'm tasked with:
"In the following sequence, each number (except the first two) is the sum of the previous two numbers: 0, 1, 1, 2, 3, 5, 8, 13, .... This sequence is known as the Fibonacci sequence.
We speak of the i'th element of the sequence (starting at 0)-- thus the 0th element is 0, the 1st element is 1, the 2nd element is 1, the 3rd element is 2 and so on. Given the positive integer n, associate the nth value of the fibonacci sequence with the variable result. For example, if n is associated with the value 8 then result would be associated with 21."
I've found a line of code that successfully implements this sequence:
x = 1
y = 1
n = 0
while n < 1000:
x = y
y = n
n = x + y
print(n)
But this obviously doesn't include the variable "result", nor does it associate the result with n. Any help on how to go about this?
"In the following sequence, each number (except the first two) is the sum of the previous two numbers: 0, 1, 1, 2, 3, 5, 8, 13, .... This sequence is known as the Fibonacci sequence.
We speak of the i'th element of the sequence (starting at 0)-- thus the 0th element is 0, the 1st element is 1, the 2nd element is 1, the 3rd element is 2 and so on. Given the positive integer n, associate the nth value of the fibonacci sequence with the variable result. For example, if n is associated with the value 8 then result would be associated with 21."
I've found a line of code that successfully implements this sequence:
x = 1
y = 1
n = 0
while n < 1000:
x = y
y = n
n = x + y
print(n)
But this obviously doesn't include the variable "result", nor does it associate the result with n. Any help on how to go about this?