How to resolve the algorithm Stern-Brocot sequence step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Stern-Brocot sequence step by step in the 11l programming language

Table of Contents

Problem Statement

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

Show your output on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Stern-Brocot sequence step by step in the 11l programming language

Source code in the 11l programming language

F stern_brocot(predicate = series -> series.len < 20)
   V sb = [1, 1]
   V i = 0
   L predicate(sb)
      sb [+]= [sum(sb[i .< i + 2]), sb[i + 1]]
      i++
   R sb

V n_first = 15
print(("The first #. values:\n  ".format(n_first))‘ ’stern_brocot(series -> series.len < :n_first)[0 .< n_first])
print()

V n_max = 10
L(n_occur) Array(1 .. n_max) [+] [100]
   print((‘1-based index of the first occurrence of #3 in the series:’.format(n_occur))‘ ’(stern_brocot(series -> @n_occur !C series).index(n_occur) + 1))
print()

V n_gcd = 1000
V s = stern_brocot(series -> series.len < :n_gcd)[0 .< n_gcd]
assert(all(zip(s, s[1..]).map((prev, this) -> gcd(prev, this) == 1)), ‘A fraction from adjacent terms is reducible’)

  

You may also check:How to resolve the algorithm Prime decomposition step by step in the Befunge programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the Python programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Crystal programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the zkl programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Erlang programming language