Posts: 10
Threads: 2
Joined: Feb 2019
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?
Posts: 3,458
Threads: 101
Joined: Sep 2016
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
Posts: 10
Threads: 2
Joined: Feb 2019
But what if I want to know the 5th number bigger than 9 in the fibonacci sequence?
Posts: 4,220
Threads: 97
Joined: Sep 2016
(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).
Posts: 10
Threads: 2
Joined: Feb 2019
(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
Posts: 10
Threads: 2
Joined: Feb 2019
Feb-26-2019, 11:43 AM
(This post was last modified: Feb-26-2019, 04:28 PM by ichabod801.)
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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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
Posts: 10
Threads: 2
Joined: Feb 2019
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)
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 10
Threads: 2
Joined: Feb 2019
Feb-27-2019, 05:28 PM
(This post was last modified: Feb-27-2019, 05:28 PM by PythonNoob99.)
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)
|