r""" Python module for OEIS sequence number A070868. a(1) = a(2) = 1; for n > 2, a(n) = a(a(n-1)) + a(n+1 - 2*a(n-1)). Examples of use. ----------------------------------------------------------------------- >>> from a070868 import * >>> print a070868_list(18) [1, 1, 2, 2, 2, 3, 3, 4, 3, 4, 4, 4, 5, 4, 6, 5, 6, 6] >>> print a070868_offset 1 >>> for x in a070868_list_pairs(6): ... print x ... (1, 1) (2, 1) (3, 2) (4, 2) (5, 2) (6, 3) >>> print a070868(100) 20 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a070868_offset', 'a070868_list', 'a070868_list_pairs', 'a070868', 'a070868_gen') __author__ = 'Nick Hobson <nickh@qbyte.org>' a070868_offset = offset = 1 def a070868_gen(): """Generator function for OEIS sequence A070868.""" a = {1:1, 2:1} yield a[1] yield a[2] for n in count(3): a[n] = a[a[n-1]] + a[n+1 - 2*a[n-1]] yield a[n] def a070868_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(a070868_gen(), n)) def a070868_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), a070868_gen())) def a070868(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(a070868_gen(), n-offset, n-offset+1)).pop()