Stern-Brocot sequence

From Rosetta Code
Revision as of 08:42, 7 December 2014 by rosettacode>Paddy3118 (New draft task and Python solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Stern-Brocot sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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
2. Start by considering the second member of the sequence
3. 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
4. Append the considered member of the sequence to the end of the sequence.
    1, 1, 2, 1
5. Consider the next member of the series, (the third member i.e. 2)
6. GOTO 3

Expanding another loop we get:

7. 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
8. Append the considered member of the sequence to the end of the sequence.
    1, 1, 2, 1, 3, 2
9. Consider the next member of the series, (the fourth member i.e. 1)

The task is to:

  1. Create a function/method/subroutine/procedure/... to generate the Stern-Brocot sequence of integers.
  2. 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)
  3. Show the (1-based) index of where the numbers 1-to-10 first appears in the sequence.
  4. Show the (1-based) index of where the number 100 first appears in the sequence.
  5. 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 the page.

Ref:

Python

<lang python></lang>

Output: