Skip to content

Instantly share code, notes, and snippets.

@azhai
Created July 3, 2014 12:29
Show Gist options
  • Save azhai/4f115d867faeffbf2e86 to your computer and use it in GitHub Desktop.
Save azhai/4f115d867faeffbf2e86 to your computer and use it in GitHub Desktop.
判断一组数中哪些是斐波那契数
#-*- coding:utf-8 -*-
from decimal import Decimal, getcontext
getcontext().prec = 50
PHI = Decimal(0.5) * (Decimal(5).sqrt() + 1)
def rdiff(n):
n = Decimal(n)
a = PHI * n
return Decimal(round(a)) * n - a * n
def is_fib(n):
return n <= 3 or -1 < rdiff(n) < 1
class Fibo:
def __init__(self):
self.a = 0L
self.b = 1L
def next(self):
self.a, self.b = self.b, self.a + self.b
return self.a
def fib_test():
fib = Fibo()
n = 0L
while n < 10L**20:
n = fib.next()
print n, rdiff(n)
if __name__ == '__main__':
n = int(raw_input())
nums = [int(raw_input()) for i in range(n)]
fib = Fibo()
curr = 0
result = {}
for num in sorted(nums):
while curr < num:
curr = fib.next()
result[num] = "IsFibo" if curr == num else "IsNotFibo"
for num in nums:
print result[num]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment