For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the Fibonacci sequence.

 The first and second members of the sequence are both 1:
     1, 1
 Start by considering the second member of the sequence
 Sum the considered member of the sequence and its precedent, (1 + 1) = 2, and append it to the end of the sequence:
     1, 1, 2
 Append the considered member of the sequence to the end of the sequence:
     1, 1, 2, 1
 Consider the next member of the series, (the third member i.e. 2)
 GOTO 3
 
         ─── Expanding another loop we get: ───

 Sum the considered member of the sequence and its precedent, (2 + 1) = 3, and append it to the end of the sequence:
     1, 1, 2, 1, 3
 Append the considered member of the sequence to the end of the sequence:
     1, 1, 2, 1, 3, 2
 Consider the next member of the series, (the fourth member i.e. 1)


The task is to

 Create a function/method/subroutine/procedure/... to generate the Stern-Brocot sequence of integers using the method outlined above.
 Show the first fifteen members of the sequence. (This should be: 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4)
 Show the (1-based) index of where the numbers 1-to-10 first appears in the sequence.
 Show the (1-based) index of where the number 100 first appears in the sequence.
 Check that the greatest common divisor of all the two consecutive members of the series up to the 1000th member, is always one.

Show your output on this page.



Related tasks

   Fusc sequence.
   Continued fraction/Arithmetic


Ref

 Infinite Fractions - Numberphile (Video).
 Trees, Teeth, and Time: The mathematics of clock making. 
 A002487 The On-Line Encyclopedia of Integer Sequences.


' version 02-03-2019
' compile with: fbc -s console
 
#Define max 2000
 
Dim Shared As UInteger stern(max +2)
 
Sub stern_brocot
 
    stern(1) = 1
    stern(2) = 1
 
    Dim As UInteger i = 2 , n = 2, ub = UBound(stern)
 
    Do While i < ub
        i += 1
        stern(i) = stern(n) + stern(n -1)
        i += 1
        stern(i) = stern(n)
        n += 1
    Loop
 
End Sub
 
Function gcd(x As UInteger, y As UInteger) As UInteger
 
    Dim As UInteger t
 
    While y
        t = y
        y = x Mod y
        x = t
    Wend
 
    Return x
 
End Function
 
' ------=< MAIN >=------
 
Dim As UInteger i
 
stern_brocot
 
Print "The first 15 are: " ;
For i = 1 To 15
    Print stern(i); " ";
Next
 
Print : Print
Print "  Index   First nr."
Dim As UInteger d = 1
For i = 1 To max
    If stern(i) = d Then
        Print Using " ######"; i; stern(i)
        d += 1
        If d = 11 Then d = 100
        If d = 101 Then Exit For
        i = 0
    End If
Next
 
Print : Print
d = 0
For i = 1 To 1000
    If gcd(stern(i), stern(i +1)) <> 1 Then
        d = gcd(stern(i), stern(i +1))
        Exit For
    End If
Next
 
If d = 0 Then
    Print "GCD of two consecutive members of the series up to the 1000th member is 1"
Else
    Print "The GCD for index "; i; " and "; i +1; " = "; d
End If
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End