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

Published on 12 May 2024 09:40 PM

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

Source code in the miranda programming language

main :: [sys_message]
main = [Stdout (lay
         ["First 15: " ++ show first15,
          "Indices of first 1..10: " ++ show idx10,
          "Index of first 100: " ++ show idx100,
          "The GCDs of the first 1000 pairs are all 1: " ++ show allgcd])]

first15 :: [num]
first15 = take 15 stern

idx10 :: [num]
idx10 = [find num stern | num<-[1..10]]

idx100 :: num
idx100 = find 100 stern

allgcd :: bool
allgcd = and [gcd a b = 1 | (a, b) <- take 1000 (zip2 stern (tl stern))]


|| Stern-Brocot sequence
stern :: [num]
stern = 1 : 1 : concat (map consider (zip2 stern (tl stern)))
        where consider (prev,item) = [prev + item, item]

|| Supporting functions
gcd :: num->num->num
gcd a 0 = a
gcd a b = gcd b (a mod b)

find :: *->[*]->num
find item = find' 1
            where find' idx []     = 0
                  find' idx (a:as) = idx, if a = item
                                   = find' (idx + 1) as, otherwise

  

You may also check:How to resolve the algorithm CRC-32 step by step in the Go programming language
You may also check:How to resolve the algorithm Magnanimous numbers step by step in the BCPL programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the jq programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the AWK programming language
You may also check:How to resolve the algorithm SHA-1 step by step in the D programming language