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

Published on 12 May 2024 09:40 PM

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

Source code in the amazing programming language

#include 
#include 

DEF-MAIN(argv,argc)
   CLR-SCR
   SET( amount, 1200 )
   DIM(amount) AS-ONES( Stern )

  /* Generate Stern-Brocot sequence: */
   GOSUB( Generate Sequence )
   PRNL( "Find 15 first: ", [1:19] CGET(Stern) )

  /* show Stern-Brocot sequence: */
   SET( i, 1 ), ITERATE( ++i, LE?(i,10), \
                         PRN( "First ",i," at "), {i} GOSUB( Find First ), PRNL )
   PRN( "First 100 at "), {100} GOSUB( Find First ), PRNL

  /* check GCD: */
   ODD-POS, CGET(Stern), EVEN-POS, CGET(Stern), COMP-GCD, GET-SUMMATORY, DIV-INTO( DIV(amount,2) ) 

   IF ( IS-EQ?(1), PRNL("The GCD of every pair of adjacent elements is 1"),\
                   PRNL("Stern-Brocot Sequence is wrong!") )
END

RUTINES

DEF-FUN(Find First, n )
RET ( SCAN(1, n, Stern) )
    
DEF-FUN(Generate Sequence)
   SET(i,2)
   FOR( LE?(i, DIV(amount,2)), ++i )
      [i] GET( Stern ), [ MINUS-ONE(i) ] GET( Stern ), ADD-IT
      [ SUB(MUL(i,2),1) ] CPUT( Stern )
      [i] GET( Stern ), [MUL(i,2)] CPUT( Stern )
   NEXT
RET

  

You may also check:How to resolve the algorithm N'th step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Leap year step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Rosetta Code/Count examples step by step in the Java programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Ring programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Ceylon programming language