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

Published on 12 May 2024 09:40 PM

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

Source code in the algol-m programming language

begin
integer array S[1:1200];
integer i,ok;

integer function gcd(a,b);
integer a,b;
gcd := 
    if a>b then gcd(a-b,b)
    else if a
    else a;
       
integer function first(n);
integer n;
begin
    integer i;
    i := 1;
    while S[i]<>n do i := i + 1;
    first := i;
end;
    
S[1] := S[2] := 1;
for i := 2 step 1 until 600 do
begin
    S[i*2-1] := S[i] + S[i-1];
    S[i*2] := S[i];
end;

write("First 15 numbers:");
for i := 1 step 1 until 15 do 
begin
    if i-i/5*5=1 then write(S[i]) else writeon(S[i]);
end;

write("");
write("First occurrence:");
for i := 1 step 1 until 10 do write(i, " at", first(i));
write(100, " at", first(100));

ok := 1;
for i := 1 step 1 until 999 do
begin
    if gcd(S[i], S[i+1]) <> 1 then
    begin
        write("gcd",S[i],",",S[i+1],"<> 1");
        ok := 0;
    end;
end;
if ok = 1 then write("The GCD of each pair of consecutive members is 1.");
end

  

You may also check:How to resolve the algorithm Greatest common divisor step by step in the Kotlin programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the Java programming language
You may also check:How to resolve the algorithm Safe primes and unsafe primes step by step in the Sidef programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the Raku programming language
You may also check:How to resolve the algorithm Pathological floating point problems step by step in the zkl programming language