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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Stern-Brocot sequence step by step in the Raku 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 Raku programming language

Source code in the raku programming language

constant @Stern-Brocot = 1, 1, { |(@_[$_ - 1] + @_[$_], @_[$_]) given ++$ } ... *;
 
put @Stern-Brocot[^15];
 
for flat 1..10, 100 -> $ix {
    say "First occurrence of {$ix.fmt('%3d')} is at index: {(1+@Stern-Brocot.first($ix, :k)).fmt('%4d')}";
}
 
say so 1 == all map ^1000: { [gcd] @Stern-Brocot[$_, $_ + 1] }


  

You may also check:How to resolve the algorithm Factorial step by step in the SSEM programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the Maxima programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Processing programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Go programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the ooRexx programming language