r""" Python module for OEIS sequence number A008336. a(n+1) = a(n)/n if n|a(n) else a(n)*n. Examples of use. ----------------------------------------------------------------------- >>> from a008336 import * >>> print a008336_list(12) [1, 1, 2, 6, 24, 120, 20, 140, 1120, 10080, 1008, 11088] >>> print a008336_offset 1 >>> for x in a008336_list_pairs(6): ... print x ... (1, 1) (2, 1) (3, 2) (4, 6) (5, 24) (6, 120) >>> print a008336(5) 24 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a008336_offset', 'a008336_list', 'a008336_list_pairs', 'a008336', 'a008336_gen') __author__ = 'Nick Hobson ' a008336_offset = offset = 1 def a008336_gen(): """Generator function for OEIS sequence A008336.""" x = 1 for m in count(1): yield x x = x/m if x%m == 0 else x*m def a008336_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(a008336_gen(), n)) def a008336_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), a008336_gen())) def a008336(n): """Returns the term with index n >= 1; offset 1.""" if n < offset: raise ValueError, 'Input must be an integer >= offset = ' + str(offset) return list(islice(a008336_gen(), n-offset, n-offset+1)).pop()