r"""
Python module for OEIS sequence number A000178.

Superfactorials: product of first n factorials.

Examples of use.
-----------------------------------------------------------------------
>>> from a000178 import *
>>> print a000178_list(7)
[1, 1, 2, 12, 288, 34560, 24883200]
>>> print a000178_offset
0
>>> for x in a000178_list_pairs(6):
...     print x
...
(0, 1)
(1, 1)
(2, 2)
(3, 12)
(4, 288)
(5, 34560)
>>> a000178_list_upto(10**6)
[1, 1, 2, 12, 288, 34560]
>>> print a000178(3)
12
-----------------------------------------------------------------------
"""

from itertools import islice, izip, takewhile, count

__all__ = ('a000178_offset', 'a000178_list', 'a000178_list_pairs', 'a000178_list_upto', 'a000178', 'a000178_gen')
__author__ = 'Nick Hobson <nickh@qbyte.org>'

a000178_offset = offset = 0

def a000178_gen():
    """Generator function for OEIS sequence A000178."""
    x, fact = 1, 1
    for n in count(1):
        yield x
        fact *= n
        x *= fact

def a000178_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(a000178_gen(), n))

def a000178_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), a000178_gen()))

def a000178_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, a000178_gen()))

def a000178(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(a000178_gen(), n-offset, n-offset+1)).pop()