r""" Python module for OEIS sequence number A005229. a(1) = a(2) = 1; for n > 2, a(n) = a(a(n-2)) + a(n-a(n-2)). Examples of use. ----------------------------------------------------------------------- >>> from a005229 import * >>> print a005229_list(16) [1, 1, 2, 3, 3, 4, 5, 6, 6, 7, 7, 8, 9, 10, 10, 11] >>> print a005229_offset 1 >>> for x in a005229_list_pairs(6): ... print x ... (1, 1) (2, 1) (3, 2) (4, 3) (5, 3) (6, 4) >>> print a005229(6) 4 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a005229_offset', 'a005229_list', 'a005229_list_pairs', 'a005229', 'a005229_gen') __author__ = 'Nick Hobson <nickh@qbyte.org>' a005229_offset = offset = 1 def a005229_gen(): """Generator function for OEIS sequence A005229.""" a = {1:1, 2:1} yield a[1] yield a[2] for n in count(3): a[n] = a[a[n-2]] + a[n-a[n-2]] yield a[n] def a005229_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(a005229_gen(), n)) def a005229_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), a005229_gen())) def a005229(n): """Returns the term with index n >= 1; offset 1.""" if n < offset: raise ValueError, 'Input must be an integer >= offset = ' + str(offset) return list(islice(a005229_gen(), n-offset, n-offset+1)).pop()