r""" Python module for OEIS sequence number A000100. a(n) = number of compositions of n in which the maximum part size is 3. a(n) = 2*a(n-1) + a(n-2) - a(n-3) - 2*a(n-4) - a(n-5). Examples of use. ----------------------------------------------------------------------- >>> from a000100 import * >>> print a000100_list(15) [0, 0, 0, 1, 2, 5, 11, 23, 47, 94, 185, 360, 694, 1328, 2526] >>> print a000100_offset 0 >>> for x in a000100_list_pairs(6): ... print x ... (0, 0) (1, 0) (2, 0) (3, 1) (4, 2) (5, 5) >>> a000100_list_upto(1000) [0, 0, 0, 1, 2, 5, 11, 23, 47, 94, 185, 360, 694] >>> print a000100(3) 1 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile, count __all__ = ('a000100_offset', 'a000100_list', 'a000100_list_pairs', 'a000100_list_upto', 'a000100', 'a000100_gen') __author__ = 'Nick Hobson ' a000100_offset = offset = 0 def a000100_gen(): """Generator function for OEIS sequence A000100.""" a = [0, 0, 0, 1, 2] for x in a: yield x for n in count(5): a[n%5] = 2*a[(n-1)%5] + a[(n-2)%5] - a[(n-3)%5] - 2*a[(n-4)%5] - a[(n-5)%5] yield a[n%5] def a000100_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(a000100_gen(), n)) def a000100_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), a000100_gen())) def a000100_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, a000100_gen())) def a000100(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(a000100_gen(), n-offset, n-offset+1)).pop()