diff --git a/allalgorithms/numeric/CheckFibonacci.py b/allalgorithms/numeric/CheckFibonacci.py new file mode 100644 index 0000000..271071f --- /dev/null +++ b/allalgorithms/numeric/CheckFibonacci.py @@ -0,0 +1,18 @@ +# -*- coding: UTF-8 -*- +# +# Check fibonacci number +# The All â–²lgorithms library for python +# +# Contributed by: Natan Lucena +# Github: @NatanLucena +# + +import math + +def isPerfectSquare(x): + s = int(math.sqrt(x)) + return s*s == x + +def isFibonacci(n): + + return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)