Created
September 30, 2012 17:34
-
-
Save JonCooperWorks/3807819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/3808441