How to resolve the algorithm Klarner-Rado sequence step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Klarner-Rado sequence step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

Klarner-Rado sequences are a class of similar sequences that were studied by the mathematicians David Klarner and Richard Rado. The most well known is defined as the thinnest strictly ascending sequence K which starts 1, then, for each element n, it will also contain somewhere in the sequence, 2 × n + 1 and 3 × n + 1.

So, the sequence K starts with 1. Set n equal to the first element 1; the sequence will also contain 2 × n + 1 and 3 × n + 1, or 3 and 4. Set n equal to the next element: 3, somewhere in the sequence it will contain 2 × n + 1 and 3 × n + 1, or 7 and 10. Continue setting n equal to each element in turn to add to the sequence.

Preferably without needing to find an over abundance and sorting.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Klarner-Rado sequence step by step in the ALGOL 68 programming language

Source code in the algol programming language

BEGIN # find elements of the Klarner-Rado sequence                           #
      #    - if n is an element, so are 2n + 1 and 3n + 1                    #
    INT max element = 100 000 000;
    [ 0 : max element ]BOOL kr; FOR i FROM LWB kr TO UPB kr DO kr[ i ] := FALSE OD;
    INT n21      := 3;                    # next 2n+1 value                  #
    INT n31      := 4;                    # next 3n+1 value                  #
    INT p2       := 1;                    # the n for the next 2n+1 value    #
    INT p3       := 1;                    # the n for the next 3n+1 value    #
    kr[ 1 ]      := TRUE;
    INT kr count := 0;
    INT max count = 1 000 000;
    FOR i WHILE kr count < max count DO
        IF i = n21 THEN                   # found the next 2n+1 value        #
            IF kr[ p2 ] THEN
                kr[ i ] := TRUE
            FI;
            p2  +:= 1;
            n21 +:= 2
        FI;
        IF i = n31 THEN                   # found the next 3n+1 value        #
            IF kr[ p3 ] THEN
                kr[ i ] := TRUE
            FI;
            p3  +:= 1;
            n31 +:= 3
        FI;
        IF kr[ i ] THEN
            kr count +:= 1;
            IF   kr count <= 100 THEN
                print( ( " ", whole( i, -3 ) ) );
                IF kr count MOD 20 = 0 THEN print( ( newline ) ) FI
            ELIF kr count =     1 000 THEN
                print( ( "The     thousandth element is ", whole( i, -8 ), newline ) )
            ELIF kr count =    10 000 THEN
                print( ( "The ten thousandth element is ", whole( i, -8 ), newline ) )
            ELIF kr count =   100 000 THEN
                print( ( "The 100-thousandth element is ", whole( i, -8 ), newline ) )
            ELIF kr count = 1 000 000 THEN
                print( ( "The      millionth element is ", whole( i, -8 ), newline ) )
            FI
        FI
    OD
END

  

You may also check:How to resolve the algorithm ISBN13 check digit step by step in the D programming language
You may also check:How to resolve the algorithm Monads/List monad step by step in the Clojure programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Frink programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the ALGOL W programming language