r""" Python module for OEIS sequence number A126086. Number of paths from (0,0,0) to (n,n,n) such that at each step: (i) at least one coordinate increases, (ii) no coordinate decreases, (iii) no coordinate increases by more than 1, and (iv) all coordinates are integers. Examples of use. ----------------------------------------------------------------------- >>> from a126086 import * >>> print a126086_list(7) [1, 13, 409, 16081, 699121, 32193253, 1538743249L] >>> print a126086_offset 0 >>> for x in a126086_list_pairs(6): ... print x ... (0, 1) (1, 13) (2, 409) (3, 16081) (4, 699121) (5, 32193253) >>> a126086_list_upto(10**6) [1, 13, 409, 16081, 699121] >>> print a126086(5) 32193253 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile, count __all__ = ('a126086_offset', 'a126086_list', 'a126086_list_pairs', 'a126086_list_upto', 'a126086', 'a126086_gen') __author__ = 'Nick Hobson ' a126086_offset = offset = 0 def a126086_gen(): """Generator function for OEIS sequence A126086.""" a = [1, 13, 409, 16081] for x in a: yield x for n in count(4): a[n%4] = ((55*n**2 - 51*n + 9)*a[(n-1)%4] + 3*(37*n**2 - 150*n + 147)*a[(n-2)%4] - (n-3)*(5*n-12)*a[(n-3)%4] + (2*(n-3)**2)*a[(n-4)%4]) / n**2 yield a[n%4] def a126086_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(a126086_gen(), n)) def a126086_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), a126086_gen())) def a126086_list_upto(m): """Returns a list of all terms not exceeding m >= 0.""" if m < 0: raise ValueError, 'Input must be a non-negative integer' return list(takewhile(lambda t: t <= m, a126086_gen())) def a126086(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(a126086_gen(), n-offset, n-offset+1)).pop()