Python Forum
Assistance with Fibonacci code
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assistance with Fibonacci code
#1
hello There,

There is an "logic" error on my code, and I have not figured it out, what it is!!

Code:

#Fibonacci numbers

x = 1
y = 1
z = 0

while z < 1000:
x = y
y = z
z = x + y
print(z)
Why is it printing above 1000? What am I missing here? (Note, I am still very new to Python)

any help appreciated

Cheers
Colombian1976
Reply
#2
Assuming your code is indented like this:

x = 1
y = 1
z = 0
 
while z < 1000:
    x = y
    y = z
    z = x + y
    print(z)
It prints one number over 1000 (1597). That's because z < 1000 is only checked at the while statement. It goes through all of the indented code, goes back to the while statement, and then checks the condition. So on the last iteration of the loop, it adds 610 and 987 and puts that in z, prints z, and then sees that z is over 1000 and stops the loop.

The easiest way to fix this is to put print(z) at the top of the loop, but you have to swap the initial values of y and z for that to work.

Have you done tuple assignment yet? That's very helpful for Fibonacci numbers.

x, y = y, x+y
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
You have missed the increment of control variable 'z' in your program. That is why it printing above 1000 elements. In Fact the program prints the elements continuously until you stop the running.

"while" loop executes a block of code associated with it till the given condition becomes false. In your program the condition is 0<1000 which means the block of executed until 0 is less than 1000. So you should add the increment statement in the code-block.
like

while z < 1000:
x = y
y = z
z = x + y
print(z)
z=z+1

In this corrected program z incremented each time and the program will stop printing when z=1001.

For further details visit my webpage
https://wallbreakerhero.blogspot.com/202...eries.html
Reply
#4
11 is a very strange number.

Fibonacci is a very strange sequence.

For every Fibonacci sequence of length 10, the sum of the sequence is equal to the 7th term times 11!

As long as you enter 10 in myApp() the sum of the sequence is equal to the 7th term times 11!

def myApp(num):
    if num < 7:
        print("Please enter a number >= 7.")
        return
    one = random.randint(1, 100)
    two = random.randint(1, 100)
    print(f'first number is {one}, the second number is {two}.')
    myfibo = [one, two]
    for n in range(2, num):
        nu = myfibo[n-2] + myfibo[n-1]
        myfibo.append(nu)
    for i in myfibo:
        print(i)
    print(f'the length of myfibo = {len(myfibo)}')
    print(f'The sum of myfibo is {sum(myfibo)}')
    print(f'{myfibo[6]} x 11 = {myfibo[6] * 11}')
    return myfibo
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fibonacci sequence problem is confusing me adam10e10 2 3,538 Apr-04-2019, 10:55 AM
Last Post: perfringo
  Help needed on Fibonacci series nvakada 1 2,492 Apr-25-2018, 07:12 PM
Last Post: micseydel
  Linear Fibonacci sequence Tawnwen 1 3,308 Feb-27-2018, 08:01 AM
Last Post: nilamo
  Help with fibonacci sequence masonmoore93 2 8,019 Oct-01-2017, 12:56 AM
Last Post: masonmoore93
  Fibonacci sequence logic 3 4,782 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