r""" Python module for OEIS sequence number A000213. Tribonacci numbers: a(n) = a(n-1) + a(n-2) + a(n-3), with a(0) = a(1) = a(2) = 1. Examples of use. ----------------------------------------------------------------------- >>> from a000213 import * >>> print a000213_list(15) [1, 1, 1, 3, 5, 9, 17, 31, 57, 105, 193, 355, 653, 1201, 2209] >>> print a000213_offset 0 >>> for x in a000213_list_pairs(6): ... print x ... (0, 1) (1, 1) (2, 1) (3, 3) (4, 5) (5, 9) >>> a000213_list_upto(1000) [1, 1, 1, 3, 5, 9, 17, 31, 57, 105, 193, 355, 653] >>> print a000213(3) 3 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile __all__ = ('a000213_offset', 'a000213_list', 'a000213_list_pairs', 'a000213_list_upto', 'a000213', 'a000213_gen') __author__ = 'Nick Hobson ' a000213_offset = offset = 0 def a000213_gen(): """Generator function for OEIS sequence A000213.""" x = y = z = 1 while True: yield x x, y, z = y, z, x + y + z def a000213_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(a000213_gen(), n)) def a000213_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), a000213_gen())) def a000213_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, a000213_gen())) def a000213(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(a000213_gen(), n-offset, n-offset+1)).pop()