r""" Python module for OEIS sequence number A000172. Franel number a(n) = Sum C(n,k)^3, k=0..n. (n+1)^2 * a(n+1) = (7n^2+7n+2) * a(n) + 8n^2 * a(n-1). Examples of use. ----------------------------------------------------------------------- >>> from a000172 import * >>> print a000172_list(10) [1, 2, 10, 56, 346, 2252, 15184, 104960, 739162, 5280932] >>> print a000172_offset 0 >>> for x in a000172_list_pairs(6): ... print x ... (0, 1) (1, 2) (2, 10) (3, 56) (4, 346) (5, 2252) >>> a000172_list_upto(10**6) [1, 2, 10, 56, 346, 2252, 15184, 104960, 739162] >>> print a000172(3) 56 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile, count __all__ = ('a000172_offset', 'a000172_list', 'a000172_list_pairs', 'a000172_list_upto', 'a000172', 'a000172_gen') __author__ = 'Nick Hobson ' a000172_offset = offset = 0 def a000172_gen(): """Generator function for OEIS sequence A000172.""" x, y = 1, 2 for n in count(2): yield x x, y = y, ((7*n**2 - 7*n + 2)*y + (8*(n-1)**2)*x) / n**2 def a000172_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(a000172_gen(), n)) def a000172_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), a000172_gen())) def a000172_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, a000172_gen())) def a000172(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(a000172_gen(), n-offset, n-offset+1)).pop()