r""" Python module for OEIS sequence number A001333. Numerators of continued fraction convergents to sqrt(2). Examples of use. ----------------------------------------------------------------------- >>> from a001333 import * >>> print a001333_list(12) [1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119] >>> print a001333_offset 0 >>> for x in a001333_list_pairs(6): ... print x ... (0, 1) (1, 1) (2, 3) (3, 7) (4, 17) (5, 41) >>> print a001333(5) 41 ----------------------------------------------------------------------- """ from itertools import islice, izip __all__ = ('a001333_offset', 'a001333_list', 'a001333_list_pairs', 'a001333', 'a001333_gen') __author__ = 'Nick Hobson ' a001333_offset = offset = 0 def a001333_gen(): """Generator function for OEIS sequence A001333.""" x, y = 1, 1 while True: yield x x, y = y, 2*y + x def a001333_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(a001333_gen(), n)) def a001333_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), a001333_gen())) def a001333(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(a001333_gen(), n-offset, n-offset+1)).pop()