Skip to content

Instantly share code, notes, and snippets.

@davidawad
Last active August 29, 2015 14:05
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 davidawad/c8cf90ad57a69f00770c to your computer and use it in GitHub Desktop.
Save davidawad/c8cf90ad57a69f00770c to your computer and use it in GitHub Desktop.
Solution to the Is Fibo Challenge on Hackerrank in Python

I'll make this later##

====enjoy====

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
@swe20144
Copy link

i think you can omit this part "or n==2"

@cshjin
Copy link

cshjin commented Feb 16, 2015

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