""" Python module for OEIS sequence number A000166. Examples of use. ---------------------------------------------------------------- >>> from a000166 import * >>> print a000166_list(6) [1, 0, 1, 2, 9, 44] >>> for x in a000166_list_pairs(6): ... print x ... (0, 1) (1, 0) (2, 1) (3, 2) (4, 9) (5, 44) >>> print a000166_list_upto(10**6) [1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496] >>> print a000166(5) 44 ---------------------------------------------------------------- """ from itertools import islice, takewhile __author__ = 'Nick Hobson ' def a000166_gen(): """Generator function for OEIS sequence A000166. Uses the recurrence equation a(0)=1; a(n)=na(n-1) + (-1)^n, for n > 0. """ x, n = 1, 0 yield x while True: n += 1 x = n*x + (-1)**n yield x def a000166_list(n): """Returns a list of the first n >= 0 terms.""" return list(islice(a000166_gen(), n)) def a000166_list_pairs(n): """Returns a list of tuples (n, a(n)) of the first n >= 0 terms.""" return list(enumerate(islice(a000166_gen(), n))) def a000166_list_upto(m): """Returns a list of all terms not exceeding m > 0.""" return list(takewhile(lambda t: t <= m, a000166_gen())) def a000166(n): """Returns the term with index n >= 0; offset 0.""" return list(islice(a000166_gen(), n, n+1)).pop()