r""" Python module for OEIS sequence number A000129. Pell numbers: a(0) = 0, a(1) = 1; for n > 1, a(n) = 2*a(n-1) + a(n-2). Also denominators of continued fraction convergents to sqrt(2). Examples of use. ----------------------------------------------------------------------- >>> from a000129 import * >>> print a000129_list(12) [0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741] >>> print a000129_offset 0 >>> for x in a000129_list_pairs(6): ... print x ... (0, 0) (1, 1) (2, 2) (3, 5) (4, 12) (5, 29) >>> print a000129(5) 29 ----------------------------------------------------------------------- """ from itertools import islice, izip __all__ = ('a000129_offset', 'a000129_list', 'a000129_list_pairs', 'a000129', 'a000129_gen') __author__ = 'Nick Hobson ' a000129_offset = offset = 0 def a000129_gen(): """Generator function for OEIS sequence A000129.""" x, y = 0, 1 while True: yield x x, y = y, 2*y + x def a000129_list(n): """Returns a list of the first n >= 0 terms.""" if n < 0: raise ValueError, 'Input must be a non-negative integer' return list(islice(a000129_gen(), n)) def a000129_list_pairs(n): """Returns a list of tuples (n, a(n)) of the first n >= 0 terms.""" if n < 0: raise ValueError, 'Input must be a non-negative integer' return list(izip(xrange(offset, n+offset), a000129_gen())) def a000129(n): """Returns the term with index n >= 0; offset 0.""" if n < offset: raise ValueError, 'Input must be an integer >= offset = ' + str(offset) return list(islice(a000129_gen(), n-offset, n-offset+1)).pop()