r""" Python module for OEIS sequence number A078759. Height of n-th term in A064389 (variation (4) of Recaman's sequence). Examples of use. ----------------------------------------------------------------------- >>> from a078759 import * >>> print a078759_list(18) [1, 2, 3, 2, 3, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6] >>> print a078759_offset 1 >>> for x in a078759_list_pairs(6): ... print x ... (1, 1) (2, 2) (3, 3) (4, 2) (5, 3) (6, 4) >>> print a078759(4) 2 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a078759_offset', 'a078759_list', 'a078759_list_pairs', 'a078759', 'a078759_gen') __author__ = 'Nick Hobson <nickh@qbyte.org>' a078759_offset = offset = 1 def a078759_gen(): """Generator function for OEIS sequence A078759.""" s, x, k = set(), 0, 0 for n in count(1): y = x m = -n j = -1 x = y + m while not (x > 0 and x not in s): m = -m if m < 0 else -(m + 1) j = -j x = y + m s.add(x) k += j yield k def a078759_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(a078759_gen(), n)) def a078759_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), a078759_gen())) def a078759(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(a078759_gen(), n-offset, n-offset+1)).pop()