Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Because "their digits are in increasing order (the numbers may have two or more equal contiguous digits)"
And '1 < 0 < 9'
And '9 < 0 < 1'
are false~
they all have the discuss button. If you don't see it, resize the window in your browser.
https://www.codewars.com/kata/the-millionth-fibonacci-kata/discuss/
sorry,not know the rules very much. some trains have discuss button in it, but not this
@zrhhust don't post code here, it can't be marked as having spoiler content, and why didn't you use that kata discourse in the first place?
I your code takes ~45s for
fib(1000000)
, then it's too slow. There's a few tests with large numbers, largest being ~1000000, and you have only 12 seconds to execute all of them.the code is very simple:
def fib(n):
"""Calculates the nth Fibonacci number"""
print(n)
a,b = 0,1
while n>1:
if n%2 == 0:
n-=2
a,b = a+b, a+2b
else:
n-=1
a,b = b, a+b
#print(n,a,b)
while n<0:
if n%2 == 0:
n+=2
a,b = 2a-b, b-a
else:
n+=1
a,b = b-a,a
#print(n,a,b)
if n==0:
return a
elif n==1:
return b
print(fib(1000000))
for fib(1000000)
real 0m43.723s
user 0m41.752s
sys 0m0.206s
fib(84362)
What
n
did you try on your computer? How long does your computer run your solution for n=-20? How long does your computer run your solution for n=1000000? Why did not you post a question in the kata discrouse?for kata "The Millionth Fibonacci Kata", my code run no more than 0.4 seconds in my computer, why get a timeout @codewars?
real 0m0.337s
user 0m0.331s
sys 0m0.005s
if i don't add print in the program, just get a timeout with nothing
if i add any print in the program, it just complains about too many print in my program
This comment is hidden because it contains spoiler information about the solution