for some reason I have to determine if a big number is a fibonacci number, so I copy some code from internet and modified it a little, it seems not operate well when it's big input. here is code:
# python program to check if x is a perfect square
import math
# A utility function that returns true if x is perfect square
def isPerfectSquare(x):
s = int(math.sqrt(x))
boo = (s*s == x);
return boo
# Returns true if n is a Fibinacci Number, else false
def isFibonacci(n):
# n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both
# is a perferct square
b = 5*n*n+4;
c = 5*n*n-4;
return isPerfectSquare(b) or isPerfectSquare(c)
# A utility function to test above functions
a = int(input("give me the number"));
print(isFibonacci(a))
when I input 610
, it output true as planed, but when I input
"215414832505658809004682396169711233230800418578767753330908886771798637"
which I know is the 343rd fibonacci number from another java program I made. it output false surprisingly. So is it because the number is too large so it makes mistakes? but I think python should be able to handle enormous big number because it is based on the memory you have? is the problem in my program or is it because it's too big input? Thx!