Ejemplo n.º 1
0
    def elementIt(self, n):
        """returns value for element N in the series as an integer"""
        #assert type(n) is IntType
        isNegThrow(n)

        if n == 0:
            return self._o
        elif n == 1:
            return self._p
        elif n == 2:
            return self._q

        f1 = self._q
        f2 = self._p  #first element f(n - 2)
        f3 = self._o  #second element f(n - 3)

        ret = 0

        for i in range(2, n):
            #t = f3   #v stores previous value

            ret = self._r * f2 + self._s * f3
            f3 = f2  #prev stores what will be previous value on next iteration
            f2 = f1
            f1 = ret

        return ret
Ejemplo n.º 2
0
    def _generator(self, n):
        #try:
        #convert any not integer type to an integer
        #n = n if type n is IntType else int(n)
        isNegThrow(n)

        for i in range(n):
            yield self.element(i)
Ejemplo n.º 3
0
    def _generator(cls, N):

        #try:
        #convert any not integer type to an integer
        #N = N if type N is IntType else int(N)
        isNegThrow(N)

        for i in range(1, N):
            yield cls.element(i)
Ejemplo n.º 4
0
    def element(self, n):
        """returns value for element N in the series as an integer"""
        #assert type(n) is IntType
        isNegThrow(n)

        if n == 0:
            return self._p
        if n == 1:
            return self._q

        return self._r * self.element(n - 1) + self._s * self.element(n - 2)
Ejemplo n.º 5
0
    def elementIt(self, n):
        """returns value for element N in the series as an integer"""
        #assert type(n) is IntType
        isNegThrow(n)

        if n == 0:
            return self._p
        elif n == 1:
            return self._q

        prev = self._p  #first element n - 2
        ret = self._q  #second element n - 1

        for i in range(1, n):
            v = prev  #v stores previous value

            prev = ret  #prev stores what will be previous value on next iteration
            ret = self._r * ret + self._s * v

        return ret