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

Second order Recaman's sequence: a(0) = 0; for n > 0,
a(n) = a(n-1) - A005132(n) if that number is positive and
not already in the sequence, otherwise a(n-1) + A005132(n).

Examples of use.
-----------------------------------------------------------------------
>>> from a123483 import *
>>> print a123483_list(16)
[0, 1, 4, 10, 8, 15, 2, 22, 34, 13, 24, 46, 36, 59, 50, 26]
>>> print a123483_offset
0
>>> for x in a123483_list_pairs(6):
...     print x
...
(0, 0)
(1, 1)
(2, 4)
(3, 10)
(4, 8)
(5, 15)
>>> print a123483(3)
10
-----------------------------------------------------------------------
"""

from itertools import islice, izip, count
from a005132 import a005132_gen

__all__ = ('a123483_offset', 'a123483_list', 'a123483_list_pairs', 'a123483', 'a005132_gen')
__author__ = 'Nick Hobson <nickh@qbyte.org>'

a123483_offset = offset = 0

def a123483_list(n):
    """Returns a list of the first n >= 0 terms of OEIS sequence A123483."""
    if n < 0: raise ValueError, 'Input must be a non-negative integer'
    return list(islice(a005132_gen(a005132_gen(count())), n))

def a123483_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), a005132_gen(a005132_gen(count()))))

def a123483(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(a005132_gen(a005132_gen(count())), n-offset, n-offset+1)).pop()