Python Forum
Help with fibonacci sequence
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with fibonacci sequence
#1
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?
Reply
#2
Quote:I've found a line of code that successfully implements this sequence
Think about the problem and write your own code.

You already have found code to generate fib numbers, so how do you get the Nth fib? How do you store the result of it in a variable named result?
Reply
#3
(Oct-01-2017, 12:36 AM)Mekire Wrote:
Quote:I've found a line of code that successfully implements this sequence
Think about the problem and write your own code.

You already have found code to generate fib numbers, so how do you get the Nth fib? How do you store the result of it in a variable named result

I modified the code slightly so I could use n as the nth number.

x = 1
y = 1
z = 0

while z < 1000:
    x = y
    y = z
    z = x + y
I suppose I would need to add something like this at the bottom:

result = z[n] #where n would be something like "8" and the program would yield "21"

But nothing I've tried seems to work. Your clue did help, so I'll keep trying
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assistance with Fibonacci code Colombian1976 3 2,878 Feb-17-2024, 12:09 PM
Last Post: Pedroski55
  Fibonacci sequence problem is confusing me adam10e10 2 3,487 Apr-04-2019, 10:55 AM
Last Post: perfringo
  Help needed on Fibonacci series nvakada 1 2,454 Apr-25-2018, 07:12 PM
Last Post: micseydel
  Linear Fibonacci sequence Tawnwen 1 3,263 Feb-27-2018, 08:01 AM
Last Post: nilamo
  Fibonacci sequence logic 3 4,713 Jun-11-2017, 08:23 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020