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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Self-describing numbers step by step in the XPL0 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 XPL0 programming language

Source code in the xpl0 programming language

code ChOut=8, IntOut=11;

func SelfDesc(N);               \Returns 'true' if N is self-describing 
int N;
int Len,        \length = number of digits in N
    I, D;
char Digit(10), Count(10);

        proc Num2Str(N);        \Convert integer N to string in Digit
        int N;
        int R;
        [N:= N/10;
        R:= rem(0);
        if N then Num2Str(N);
        Digit(Len):= R;
        Len:= Len+1;
        ];

[Len:= 0;
Num2Str(N);
for I:= 0 to Len-1 do Count(I):= 0;
for I:= 0 to Len-1 do
        [D:= Digit(I);
        if D >= Len then return false;
        Count(D):= Count(D)+1;
        ];
for I:= 0 to Len-1 do
        if Count(I) # Digit(I) then return false;
return true;
]; \SelfDesc


int N;
for N:= 0 to 100_000_000-1 do
        if SelfDesc(N) then [IntOut(0, N);  ChOut(0, ^ )]

  

You may also check:How to resolve the algorithm Variable-length quantity step by step in the xTalk programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Dart programming language
You may also check:How to resolve the algorithm Currency step by step in the zkl programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the bash programming language
You may also check:How to resolve the algorithm Death Star step by step in the J programming language