Skip to content

Instantly share code, notes, and snippets.

@JonCooperWorks
Created September 30, 2012 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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])
@JonCooperWorks
Copy link
Author

Discrete Math Homework

import math

def fib_check(l):
#Check if each number in list is fibonacci
for num in l:
#5_n^2 + 4 XOR 5_n^2-4 must be a perfect square
if math.sqrt(5_(num__2)+4) % 1 == 0 != math.sqrt(5_(num**2)-4) % 1 == 0:
return False
return True

Tests

print fib_check([1,1,2])
print fib_check([2,3,5,8])

@JSkally
Copy link

JSkally commented Sep 30, 2012

l=[1,233,8,610,21]

@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