====enjoy====
Last active
August 29, 2015 14:05
-
-
Save davidawad/c8cf90ad57a69f00770c to your computer and use it in GitHub Desktop.
Solution to the Is Fibo Challenge on Hackerrank in Python
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
def fib(n): | |
if n==0: | |
return 0 | |
if n==1 or n==2: | |
return 1 | |
return fib(n-1)+fib(n-2) | |
cases = int(input()) | |
for i in range(0,cases): | |
curr = int(input()) | |
j=0 | |
while int(fib(j)) < 10**10 : | |
##print('while ran '+str(j)+' time(s)') | |
##print('fib(j) is '+str(fib(j)) +' at this time' ) | |
##print('curr is '+str(curr) +' at this time' ) | |
if curr == fib(j): | |
print('IsFibo') | |
break | |
if curr < fib(j): | |
print('IsNotFibo') | |
break | |
j+=1 | |
i+=1 |
The code you write may cause runtime issue.
How about this:
fibo_list = [0,1]
new_one = sum(fibo_list[-2:])
while new_one < 10**10:
fibo_list.append(new_one)
new_one = sum(fibo_list[-2:])
N = int(raw_input())
for i in range(N):
num = int(raw_input())
if num in fibo_list:
print "IsFibo"
else:
print "IsNotFibo"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i think you can omit this part "or n==2"