r""" Python module for OEIS sequence number A005206. Hofstadter G-sequence: a(n) = n - a(a(n-1)). Examples of use. ----------------------------------------------------------------------- >>> from a005206 import * >>> print a005206_list(19) [0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11] >>> print a005206_offset 0 >>> for x in a005206_list_pairs(6): ... print x ... (0, 0) (1, 1) (2, 1) (3, 2) (4, 3) (5, 3) >>> print a005206(161) 100 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a005206_offset', 'a005206_list', 'a005206_list_pairs', 'a005206', 'a005206_gen') __author__ = 'Nick Hobson ' a005206_offset = offset = 0 def a005206_gen(): """Generator function for OEIS sequence A005206.""" a = {0:0} yield a[0] for n in count(1): a[n] = n - a[a[n-1]] yield a[n] def a005206_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(a005206_gen(), n)) def a005206_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), a005206_gen())) def a005206(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(a005206_gen(), n-offset, n-offset+1)).pop()