Skip to content

Instantly share code, notes, and snippets.

@JonCooperWorks
Created September 30, 2012 17:34
Show Gist options
  • Save JonCooperWorks/3807819 to your computer and use it in GitHub Desktop.
Save JonCooperWorks/3807819 to your computer and use it in GitHub Desktop.
#Discrete Math Homework
import math
def fib_check(l):
def is_fibonacci(n):
phi = 0.5 + 0.5 * math.sqrt(5.0)
a = phi * n
return n == 0 or abs(round(a) - a) < 1.0 / n
#Check if each number in list is fibonacci
if sorted(l) == l:
for num in l:
if not is_fibonacci(num):
return False
return True
return False
#Tests
print fib_check([1,1])
print fib_check([1,1,2])
print fib_check([2,3,5,8])
print fib_check([1,1,2,3,5,8])
print fib_check([1,2,5,3])
print fib_check([1,2,3,4,5,6])
@JSkally
Copy link

JSkally commented Sep 30, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment