0

Fibonacci Series between two numbers in python using while loop

import math a = input('enter a value') z = input ('enter end value') while a <= z: b = ((5 * float(a) * float(a)) + 4) c = ((5 * float(a) * float(a)) - 4) d = math.sqrt(float(b)) e = math.sqrt(float(a)) if int(d) or int(e) == int: print(a) a = (int(a) + 1) what am i doing wrong at the third last line ? I'm looking to make a while loop where it prints fibonacci numbers; I'm doing ok until the part where i need to recognise if b or c is a perfect square. so i find the sq root of b and c and check if its an integer(+ or - number without decimal) and not a float(a number with a decimal). if ether of the two are an integer it would print out the integer, add one to if and continue the loop until a >= z. but I'm not getting the results i desire where am i going wrong ? pls can any1 advice ?

12th May 2019, 1:24 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
9 Answers
+ 6
if int(d) or int(e) == int doesn't work. If anything, it would be if int(d) == int or int(e) == int. But that's wrong too. int(d) will convert any number into an integer, so if you check if int(5.3) is an integer, it will always return True, because you just converted it to an integer. But you can't use "== int" to check if a number is an integer. Use isinstance(d, int) or type(d) == int instead. However, that won't work neither because a square root will always be a float. Even sqrt(9) will return 3.0 (float) and not 3 (int). You can use if d % 1 == 0 to check if there are no decimal places: sqrt(9) % 1 == 0 # True sqrt(5) % 1 == 0 # False
12th May 2019, 2:23 PM
Anna
Anna - avatar
+ 5
You don't need all those parentheses and type conversions. A single a = int(input('enter a value')) at the beginning will make sure that the input is an integer (or float(input('...')) for a float). After that, you don't need to convert them to different numeric types anymore and can just write while a <= z, math.sqrt(a) or b = 5*a+4. I'm not sure where the error in your code is because to be honest I don't understand what you're doing :) But if d%1 == 0 or e%1 == 0 is correct to determine that at least one of the variables d or e is an integer
13th May 2019, 12:22 PM
Anna
Anna - avatar
+ 3
x % 1 checks if the number is divisible by 1 without a rest. This is true for all integers
12th May 2019, 6:32 PM
Anna
Anna - avatar
+ 1
thank you so so much ! but i dont understand this, 9%2 will be 1 ? % divides the number and gives us the remainder instead of the divident right ? how is it helping us find if we have decimals or not here ?
12th May 2019, 6:30 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
+ 1
;D thank you so much ill try it right away
12th May 2019, 6:38 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
0
import math a = input('enter a value') z = input ('enter end value') while int(a) <= int(z): b = ((5 * float(a) * float(a)) + 4) c = ((5 * float(a) * float(a)) - 4) d = math.sqrt(float(b)) e = math.sqrt(float(a)) if (d)%1 == 0 or (e)%1 == 0: print(a) a = (int(a) + 1) i think this what you suggested ? the result stops with 1 :S i try to close the windows, it says program is still runiing.. i get result: 1 thats all.. is something else wrong anywhere else ?
13th May 2019, 12:10 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
0
thank you anna for being so patient and to take the time to read the crappy code i wrote i really appreciate it a lot. so i found out online that for a number n to be a fibonacci number; 5* n square + 4 or 5*n square -4 would be a perfect square. i was trying to write a code using the while statement where the loops repeats over and over untill a is greater than or equal to z. im looking it to print ony numbers that satisfy the 5*n square+\- 4 is a perfect square perfect, while leaving out the rest. so the loop keeps repeating and at the end 1 is added to a for the condition check on the next number untill a is no longer smaller than z im sorry if im not being clear, i dont know how else to explain; i hope you understand and will be able to help me?
13th May 2019, 7:52 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
0
so this is how iwrote the code to decide if the number is a perfect square; i find the square root of it; if it has decimals it isnt a perfect square. if not, it is and that is where i have applied the %1 idea suggested by you anna. Anna sorry for the delay just learnt how to tag pple :D
14th May 2019, 3:55 AM
Akshay Ramchandra
Akshay Ramchandra - avatar
0
#fibonacci untill a number import math a = int(input('Enter a positive integer')) z = int(input('Enter termination number')) while a < z: a = a z = z b = ((5 * a * a) + 4) c = ((5 * a * a) - 4) d = math.sqrt(b) e = math.sqrt(c) if d % 1 == 0 or e % 1 == 0: print(str(a)) a = a + 1 Anna !!! i did it !!! thank you for your wonderful suggestion of using %1 !! :D Thank you for taking the time to make me understand it ! it really helped me a lot ! thank you !
17th May 2019, 9:31 AM
Akshay Ramchandra
Akshay Ramchandra - avatar