def lucasgen(P, Q):
    """
    Generates the Lucas U- and V-sequences with parameters (P, Q).
    Input: P, Q -- integers
    Output: sequence of 2-tuples of integers.  First element is a term of the
            U-sequence; second element is of the V-sequence.
    Examples:
    >>> list(islice(lucasgen(1, -1), 8))     # Fibonacci & Lucas numbers
    [(0, 2), (1, 1), (1, 3), (2, 4), (3, 7), (5, 11), (8, 18), (13, 29)]
    >>> list(islice(lucasgen(3,  2), 8))     # (2**n - 1) & (2**n + 1)
    [(0, 2), (1, 3), (3, 5), (7, 9), (15, 17), (31, 33), (63, 65), (127, 129)]
    """
    u0, u1, v0, v1 = 0, 1, 2, P
    while True:
        yield u0, v0
        u0, u1, v0, v1 = u1, P * u1 - Q * u0, v1, P * v1 - Q * v0
