0

How to write a program to check if a number is a Fibonacci number or not, just using import, while, if-else keywords?

8th Sep 2017, 1:18 PM
Ajeya Bhat
Ajeya Bhat - avatar
2 odpowiedzi
+ 3
In Python I assume? Way 1 : Using Property / Fast from math import sqrt def Square(x): # returns true if perfect square     s = int(sqrt(x))     return s*s == x def Fibonacci(n): # n is Fibonacci if 5n^2 + 4 or # 5n^2 - 4 are perfect squares. return Square(5*n*n + 4) or Square(5*n*n - 4) Way 2 : Using Loops / Slower (Relatively) def Fibonacci(n): f1 = 1, f2 = 1 f3 = 0 for i in range (1,2*n): f3 = f1 + f2 f1 = f2 f2 = f3 if f3 > n: break if f2 != n or f1 != n: return False else return True
8th Sep 2017, 2:03 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Fibonacci numbers are in the divine proportion of 1.6:1 (approx). Use this to make the program...I am not gonna tell everything 😉
8th Sep 2017, 2:00 PM
Neil Bhowmik
Neil Bhowmik - avatar