require 'set'

a = [0]
used = Set[0]
used1000 = Set[0]
foundDup = false
n = 1
while n <= 15 or not foundDup or used1000.size < 1001
    nxt = a[n - 1] - n
    if nxt < 1 or used === nxt then
        nxt = nxt + 2 * n
    end
    alreadyUsed = used === nxt
    a << nxt
    if not alreadyUsed then
        used << nxt
        if nxt >= 0 and nxt <= 1000 then
            used1000 << nxt
        end
    end
    if n == 14 then
        print , a, 
    end
    if not foundDup and alreadyUsed then
        print , n, , nxt, 
        foundDup = true
    end
    if used1000.size == 1001 then
        print , n, 
    end
    n = n + 1
end