0

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
furas
  • 134,197
  • 12
  • 106
  • 148

1 Answers1

0

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
lenik
  • 23,228
  • 4
  • 34
  • 43