The Recamán's sequence generates Natural numbers.

Starting from a(0)=0, the n'th term a(n), where n>0, is the previous term minus n i.e a(n) = a(n-1) - n but only if this is both positive and has not been previousely generated.


If the conditions don't hold then a(n) = a(n-1) + n.



Task

 Generate and show here the first 15 members of the sequence.
 Find and show here, the first duplicated number in the sequence.
 Optionally: Find and show here, how many terms of the sequence are needed until all the integers 0..1000, inclusive, are generated.


References

 A005132, The On-Line Encyclopedia of Integer Sequences.
 The Slightly Spooky Recamán Sequence, Numberphile video.
 Recamán's sequence, on Wikipedia.


Imports System
Imports System.Collections.Generic
 
Module Module1
    Sub Main(ByVal args As String())
        Dim a As List(Of Integer) = New List(Of Integer)() From { 0 },
            used As HashSet(Of Integer) = New HashSet(Of Integer)() From { 0 },
            used1000 As HashSet(Of Integer) = used.ToHashSet(),
            foundDup As Boolean = False
        For n As Integer = 1 to Integer.MaxValue
            Dim nv As Integer = a(n - 1) - n
            If nv < 1 OrElse used.Contains(nv) Then nv += 2 * n
            Dim alreadyUsed As Boolean = used.Contains(nv) : a.Add(nv)
            If Not alreadyUsed Then used.Add(nv) : If nv > 0 AndAlso nv <= 1000 Then used1000.Add(nv)
            If Not foundDup Then
                If a.Count = 15 Then _
                    Console.WriteLine("The first 15 terms of the Recamán sequence are: ({0})", String.Join(", ", a))
                If alreadyUsed Then _
                    Console.WriteLine("The first duplicated term is a({0}) = {1}", n, nv) : foundDup = True
            End If
            If used1000.Count = 1001 Then _
                Console.WriteLine("Terms up to a({0}) are needed to generate 0 to 1000", n) : Exit For
        Next
    End Sub
End Module