How to resolve the algorithm Self-describing numbers step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Self-describing numbers step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

There are several so-called "self-describing" or "self-descriptive" integers. An integer is said to be "self-describing" if it has the property that, when digit positions are labeled 0 to N-1, the digit in each position is equal to the number of times that that digit appears in the number. For example,   2020   is a four-digit self describing number:

Self-describing numbers < 100.000.000  are:     1210,   2020,   21200,   3211000,   42101000.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Self-describing numbers step by step in the ALGOL 68 programming language

Source code in the algol programming language

BEGIN

    # return TRUE if number is self describing, FALSE otherwise #
    OP SELFDESCRIBING = ( INT number )BOOL:
       BEGIN

           [10]INT counts := ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
           INT n          := number;
           INT digits     := 0;

           # count the occurances of each digit #
           WHILE
               n /= 0
           DO
               digits +:= 1;
               counts[ ( n MOD 10 ) + 1 ] +:= 1;
               n OVERAB 10
           OD;

           # construct the number that the counts would describe, #
           # if the number was self describing                    #

           INT described number := 0;
           FOR i TO digits
           DO
               described number *:= 10;
               described number +:= counts[ i ]
           OD;

           # if the described number is the input number, #
           # it is self describing #
           ( number = described number )
       END; # SELFDESCRIBING #

main: (

    FOR i TO 100 000 000
    DO
        IF SELFDESCRIBING i
        THEN
            print( ( i, " is self describing", newline ) )
        FI
    OD
)

END

  

You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the ERRE programming language
You may also check:How to resolve the algorithm Character codes step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Arithmetic derivative step by step in the Perl programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Julia programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Asymptote programming language