Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fibonnaci
#1
How do I translate this problem to Python?

The nth number bigger than x in the fibonacci secuence.

I already have

n = int(input(“Give a number”)
n2 = int(input(“Give a number”)


I only learnt for in, while, elif, else, if.

Can someone help?
Reply
#2
So you're getting the target value (what you want to be greater than in the sequence), and how many fibs beyond that point you're interested in.

The next task is to generate fibs, which you can do with a while loop.
last_number = 0
current_number = 1
while True:
    print(last_number)
    next_number = current_number + last_number
    last_number = current_number
    current_number = next_number

    # don't loop forever (just for testing, compare with user input when you do this)
    if current_number > 20:
        break
Reply
#3
But what if I want to know the 5th number bigger than 9 in the fibonacci sequence?
Reply
#4
(Feb-22-2019, 03:19 PM)PythonNoob99 Wrote: But what if I want to know the 5th number bigger than 9 in the fibonacci sequence?

Generate numbers until you get one greater than 9 (while loop), and then generate 4 more numbers (for loop).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Feb-22-2019, 03:28 PM)ichabod801 Wrote:
PythonNoob99 dateline='<a href="tel:1550848781">1550848781</a>' Wrote: But what if I want to know the 5th number bigger than 9 in the fibonacci sequence?
Generate numbers until you get one greater than 9 (while loop), and then generate 4 more numbers (for loop).
Okey, lots of thanks
Reply
#6
I have difficulties to translate what I want into the programming language.
n = int(input("number"))
n2 = int(input("number2"))

G1 = 0
G2 = 1
G3 = 0
i = 0

while G3 < n:
	G3 = G1 + G2
	G1 = G2 
	G2 = G3
print("number", G3)

    for i in range(G3):
		n2 += i
		print(n2)
if I give n = 6 then it stands for the first fibonacci number bigger than that number

But if I want the second for example I don’t know exactly how to program.
Reply
#7
You've got an indentation problem in the code shown. The three lines at the end need to be unindented one level.

Once that is fixed, that loop should be over range(n2 - 1). A for loop over range(x) will repeat x times. If you want the n2th Fibonacci number after n1, the while loop gives you the first, so that is why you subtract one.

The for loop should do exactly what the while loop is doing. It should keep generating Fibonacci numbers that same way.

Note the way you are calculating the next Fibonacci number on lines 10-12. There's nothing wrong with it. But that changing one number and then swapping is such a common thing to do that Python has a shortcut for it called tuple assignment. That is, if you have a tuple (comma separated values) on both sides of an assignment (=), the items will be assigned in order. a, b = 1, 2 assigns 1 to a and 2 to b. So you can do those three lines in one line:

G1, G2 = G2, G1 + G2
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
I understand your explanation, but I still have problems to translate.
I'm confused by the combination of the wile and for lus.
The wile lus generates the fibonacci numbers if I understand it well, and the for lus the place?

position_in_fib_sequence = int(input("Which fibonaccinumber do you want to know?"))
the_number_has_to_be_bigger_than = int(input("Bigger than?"))

G1 = 0
G2 = 1

i = 0

while G2 < position_in_fib_sequence:
    G1, G2 = G2, G1 + G2
print("The", str(position_in_fib_sequence) + "th", "fibonaccinumber bigger than", the_number_has_to_be_bigger_than, "is", G2)

    for i in range(position_in_fib_sequence-1):
        G1, G2 = G2, G1 + G2
        print(G1, G2)
Reply
#9
You did not unindent the for loop. The for loop needs to run after the while loop is finished, not every time the while loop runs.

And line 9 should check against the_number_has_to_be_bigger_than.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
What is wrong with line 9?
position_in_fib_sequence = int(input("Which fibonaccinumber do you want to know?"))
the_number_has_to_be_bigger_than = int(input("Bigger than?"))
 
G1 = 0
G2 = 1

while G2 < position_in_fib_sequence:
    G1, G2 = G2, G1 + G2
print("The", str(position_in_fib_sequence) + "th", "fibonaccinumber bigger than", the_number_has_to_be_bigger_than, "is", G2)
 
for i in range(position_in_fib_sequence-1):
    G1, G2 = G2, G1 + G2
    print(G1, G2)
Reply


Forum Jump:

User Panel Messages

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