r""" Python module for OEIS sequence number A001850. Central Delannoy numbers: Sum_{k=0..n} C(n,k)*C(n+k,k). Examples of use. ----------------------------------------------------------------------- >>> from a001850 import * >>> print a001850_list(10) [1, 3, 13, 63, 321, 1683, 8989, 48639, 265729, 1462563] >>> print a001850_offset 0 >>> for x in a001850_list_pairs(6): ... print x ... (0, 1) (1, 3) (2, 13) (3, 63) (4, 321) (5, 1683) >>> a001850_list_upto(10000) [1, 3, 13, 63, 321, 1683, 8989] >>> print a001850(5) 1683 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile, count __all__ = ('a001850_offset', 'a001850_list', 'a001850_list_pairs', 'a001850_list_upto', 'a001850', 'a001850_gen') __author__ = 'Nick Hobson ' a001850_offset = offset = 0 def a001850_gen(): """Generator function for OEIS sequence A001850.""" x, y = 1, 3 for n in count(2): yield x x, y = y, (3*(2*n-1)*y - (n-1)*x) / n def a001850_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(a001850_gen(), n)) def a001850_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), a001850_gen())) def a001850_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, a001850_gen())) def a001850(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(a001850_gen(), n-offset, n-offset+1)).pop()