r""" Python module for OEIS sequence number A070864. a(1) = a(2) = 1; for n > 2, a(n) = 2 + a(n-a(n-1)). Examples of use. ----------------------------------------------------------------------- >>> from a070864 import * >>> print a070864_list(18) [1, 1, 3, 3, 3, 5, 3, 5, 5, 5, 7, 5, 7, 5, 7, 7, 7, 9] >>> print a070864_offset 1 >>> for x in a070864_list_pairs(6): ... print x ... (1, 1) (2, 1) (3, 3) (4, 3) (5, 3) (6, 5) >>> print a070864(100) 19 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a070864_offset', 'a070864_list', 'a070864_list_pairs', 'a070864', 'a070864_gen') __author__ = 'Nick Hobson <nickh@qbyte.org>' a070864_offset = offset = 1 def a070864_gen(): """Generator function for OEIS sequence A070864.""" a = {1:1, 2:1} yield a[1] yield a[2] for n in count(3): a[n] = 2 + a[n - a[n-1]] yield a[n] def a070864_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(a070864_gen(), n)) def a070864_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), a070864_gen())) def a070864(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(a070864_gen(), n-offset, n-offset+1)).pop()