r""" Python module for OEIS sequence number A128204. a(0) = 0; a(n) = a(n-1) - (2n-1) if that number is positive and not already in the sequence, otherwise a(n) = a(n-1) + (2n-1). ('Recaman transform' (see A005132) of the odd numbers.) Examples of use. ----------------------------------------------------------------------- >>> from a128204 import * >>> print a128204_list(16) [0, 1, 4, 9, 2, 11, 22, 35, 20, 3, 22, 43, 66, 41, 14, 43] >>> print a128204_offset 0 >>> for x in a128204_list_pairs(6): ... print x ... (0, 0) (1, 1) (2, 4) (3, 9) (4, 2) (5, 11) >>> print a128204(3) 9 ----------------------------------------------------------------------- """ from itertools import islice, izip, count from a005132 import a005132_gen __all__ = ('a128204_offset', 'a128204_list', 'a128204_list_pairs', 'a128204', 'a005132_gen') __author__ = 'Nick Hobson ' a128204_offset = offset = 0 def a128204_list(n): """Returns a list of the first n >= 0 terms of OEIS sequence A128204.""" return list(islice(a005132_gen(2*x-1 for x in count()), n)) def a128204_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(a128204_offset, n+a128204_offset), a005132_gen(2*x-1 for x in count()))) def a128204(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(a005132_gen(2*x-1 for x in count()), n-a128204_offset, n-a128204_offset+1)).pop()