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.


import math, strformat, strutils
 
iterator sternBrocot(): (int, int) =
  ## Yield index and value of the terms of the sequence.
  var sequence: seq[int]
  sequence.add 1
  sequence.add 1
  var index = 1
  yield (1, 1)
  yield (2, 1)
  while true:
    sequence.add sequence[index] + sequence[index - 1]
    yield (sequence.len, sequence[^1])
    sequence.add sequence[index]
    yield (sequence.len, sequence[^1])
    inc index
 
# Fiind first 15 terms.
var res: seq[int]
for i, n in sternBrocot():
  res.add n
  if i == 15: break
echo "First 15 terms: ", res.join(" ")
echo()
 
# Find indexes for 1..10.
var indexes: array[1..10, int]
var toFind = 10
for i, n in sternBrocot():
  if n in 1..10 and indexes[n] == 0:
    indexes[n] = i
    dec toFind
    if toFind == 0: break
for n in 1..10:
  echo &"Index of first occurrence of number {n:3}: {indexes[n]:4}"
 
# Find index for 100.
var index: int
for i, n in sternBrocot():
  if n == 100:
    index = i
    break
echo &"Index of first occurrence of number 100: {index:4}"
echo()
 
# Check GCD.
var prev = 1
index = 1
for i, n in sternBrocot():
  if gcd(prev, n) != 1: break
  prev = n
  inc index
  if index > 1000: break
if index <= 1000:
  echo "Found two successive terms at index: ", index
else:
  echo "All consecutive terms up to the 1000th member have a GCD equal to one."