I'm completely confused as to why my code is returning none. I'm new and have been searching for a while and i'm still lost.
class Fibonacci:
def __init__(self, max = 0):
self.max = max
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n <= self.max: # if n is less that 0
def FIB(number):
if number == 1 or number == 2:
return 1
else:
return FIB(number - 1) + FIB(number - 2)
self.n += 1
else:
raise StopIteration
Your implementation spends more and more time every time you call __next__
. should have used an iterative method instead with the constant complexity of iteration over it:
class Fibonacci:
def __init__(self, max = 0):
self.max = max
self.a = 0
self.b = 1
def __iter__(self):
self.a = 0
self.b = 1
return self
def __next__(self):
self.a, self.b = self.b, self.a + self.b
if self.a <= self.max: # if n is less that 0
return self.a
else:
raise StopIteration
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.