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

Published on 12 May 2024 09:40 PM

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

Source code in the forth programming language

: stern ( n -- x : return N'th item of Stern-Brocot sequence)
  dup 2 >= if
      2 /mod swap if 
         dup 1+ recurse 
         swap recurse
         +
      else 
         recurse
      then 
  then 
;

: first ( n -- x : return X such that stern X = n )
  1 begin over over stern <> while 1+ repeat
  swap drop
;

: gcd ( a b -- a gcd b )
  begin swap over mod dup 0= until drop
;
 
: task 
  ( Print first 15 numbers )
  ." First 15: " 1 begin dup stern . 1+ dup 15 > until
  drop cr
  
  ( Print first occurrence of 1..10 )
  1 begin
      ." First " dup . ." at " dup first .
      1+ cr
  dup 10 > until
  drop
  
  ( Print first occurrence of 100 )
  ." First 100 at " 100 first . cr
  
  ( Check that the GCD of each adjacent pair up to 1000 is 1 )
  -1 2 begin
     dup stern over 1- stern gcd 1 =
     rot and swap
     1+
  dup 1000 > until
  swap if
     ." All GCDs are 1."
     drop
  else
     ." GCD <> 1 at: " . 
  then 
  cr  
;

task
bye


  

You may also check:How to resolve the algorithm De Bruijn sequences step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the PostScript programming language
You may also check:How to resolve the algorithm Joystick position step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the Raku programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Kotlin programming language