r""" Python module for OEIS sequence number A064289. Height of n-th term in Recaman's sequence A005132. Examples of use. ----------------------------------------------------------------------- >>> from a064289 import * >>> print a064289_list(18) [1, 2, 3, 2, 3, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6] >>> print a064289_offset 1 >>> for x in a064289_list_pairs(6): ... print x ... (1, 1) (2, 2) (3, 3) (4, 2) (5, 3) (6, 4) >>> print a064289(100) 8 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a064289_offset', 'a064289_list', 'a064289_list_pairs', 'a064289', 'a064289_gen') __author__ = 'Nick Hobson ' a064289_offset = offset = 1 def a064289_gen(): """Generator function for OEIS sequence A064289.""" s, x, k = set(), 0, 0 for n in count(1): (x, k) = (x - n, k - 1) if x - n > 0 and x - n not in s else (x + n, k + 1) s.add(x) yield k def a064289_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(a064289_gen(), n)) def a064289_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), a064289_gen())) def a064289(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(a064289_gen(), n-offset, n-offset+1)).pop()