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

a(1) = a(2) = 1; for n > 2, a(n) = a(n-1 - a(n-1)) + a(n-1 - a(n-2)).

Examples of use.
-----------------------------------------------------------------------
>>> from a070867 import *
>>> print a070867_list(18)
[1, 1, 2, 2, 2, 4, 3, 4, 4, 4, 8, 5, 5, 8, 8, 6, 8, 12]
>>> print a070867_offset
1
>>> for x in a070867_list_pairs(6):
...     print x
...
(1, 1)
(2, 1)
(3, 2)
(4, 2)
(5, 2)
(6, 4)
>>> print a070867(100)
43
-----------------------------------------------------------------------
"""

from itertools import islice, izip, count

__all__ = ('a070867_offset', 'a070867_list', 'a070867_list_pairs', 'a070867', 'a070867_gen')
__author__ = 'Nick Hobson <nickh@qbyte.org>'

a070867_offset = offset = 1

def a070867_gen():
    """Generator function for OEIS sequence A070867."""
    a = {1:1, 2:1}
    yield a[1]
    yield a[2]
    for n in count(3):
        a[n] = a[n-1 - a[n-1]] + a[n-1 - a[n-2]]
        yield a[n]

def a070867_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(a070867_gen(), n))

def a070867_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), a070867_gen()))

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