Last active
August 29, 2015 14:11
-
-
Save Bachmann1234/98edab761645ca2b39f0 to your computer and use it in GitHub Desktop.
Fib playground
This file contains hidden or 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
| from collections import deque | |
| def fibs(): | |
| f = deque([0,1], maxlen=2) | |
| while f: | |
| yield f[0] | |
| f.append(f[-1] + f[-2]) | |
| def is_fib(candidate): | |
| for fib in fibs(): | |
| if candidate == fib: | |
| return "IsFibo" | |
| elif candidate < fib: | |
| return "IsNotFibo" | |
| for _ in range(int(input())): | |
| print(is_fib(int(input()))) |
This file contains hidden or 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
| def is_fib(candidate, known_fibs=[0,1]): | |
| last_fib = known_fibs[-1] | |
| if candidate == last_fib: | |
| return "IsFibo" | |
| elif candidate < last_fib: | |
| return "IsNotFibo" | |
| else: | |
| next_fib = last_fib + known_fibs[-2] | |
| new_fibs = known_fibs + [next_fib] | |
| return is_fib(candidate, known_fibs=new_fibs) | |
| for _ in range(int(input())): | |
| print(is_fib(int(input()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment