r""" Python module for OEIS sequence number A053461. a(0) = 0; a(n) = a(n-1) - n^2 if positive and new, otherwise a(n) = a(n-1) + n^2. ('Recaman transform' (see A005132) of the squares.) Examples of use. ----------------------------------------------------------------------- >>> from a053461 import * >>> print a053461_list(16) [0, 1, 5, 14, 30, 55, 19, 68, 4, 85, 185, 64, 208, 39, 235, 10] >>> print a053461_offset 0 >>> for x in a053461_list_pairs(6): ... print x ... (0, 0) (1, 1) (2, 5) (3, 14) (4, 30) (5, 55) >>> print a053461(3) 14 ----------------------------------------------------------------------- """ from itertools import islice, izip, count from a005132 import a005132_gen __all__ = ('a053461_offset', 'a053461_list', 'a053461_list_pairs', 'a053461', 'a005132_gen') __author__ = 'Nick Hobson ' a053461_offset = offset = 0 def a053461_list(n): """Returns a list of the first n >= 0 terms of OEIS sequence A053461.""" return list(islice(a005132_gen(x*x for x in count()), n)) def a053461_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), a005132_gen(x*x for x in count()))) def a053461(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(x*x for x in count()), n, n+1)).pop()