r""" Python module for OEIS sequence numbers A057165. A057165: Addition steps in Recaman's sequence A005132. Examples of use. ----------------------------------------------------------------------- >>> from a057165 import * >>> print a057165_list(16) [1, 2, 3, 5, 6, 7, 9, 11, 13, 15, 17, 18, 19, 21, 24, 26] >>> print a057165_offset 1 >>> for x in a057165_list_pairs(6): ... print x ... (1, 1) (2, 2) (3, 3) (4, 5) (5, 6) (6, 7) >>> print a057165(100) 189 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a057165_offset', 'a057165_list', 'a057165_list_pairs', 'a057165', 'a057165_gen') __author__ = 'Nick Hobson ' a057165_offset = offset = 1 def a057165_gen(is_a057165): """Generator function for OEIS sequences A057165 and A057166.""" s, x = set(), 0 for n in count(1): (x, addition_step) = (x - n, False) if (x - n > 0 and x - n not in s) else (x + n, True) s.add(x) if addition_step == is_a057165: yield n def a057165_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(a057165_gen(True), n)) def a057165_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), a057165_gen(True))) def a057165(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(a057165_gen(True), n-1, n)).pop()