Stern-Brocot sequence

FCC link

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

  1. The first and second members of the sequence are both 1:
  • 1, 1
  1. Start by considering the second member of the sequence
  2. 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
  1. Append the considered member of the sequence to the end of the sequence:
  • 1, 1, 2, 1
  1. Consider the next member of the series, (the third member i.e. 2)
  2. GOTO 3
  • ─── Expanding another loop we get: ───
  1. 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
  1. Append the considered member of the sequence to the end of the sequence:
  • 1, 1, 2, 1, 3, 2
  1. Consider the next member of the series, (the fourth member i.e. 1)

Test

{{test}}

Console output