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

Published on 12 May 2024 09:40 PM

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

Source code in the pascal programming language

Program SelfDescribingNumber;

uses
  SysUtils;
  
function check(number: longint): boolean;
  var
    i, d: integer;
    a: string;
    count, w : array [0..9] of integer;

  begin
    a := intToStr(number);
    for i := 0 to 9 do
    begin
      count[i] := 0;
      w[i] := 0;
    end;
    for i := 1 to length(a) do
    begin
      d := ord(a[i]) - ord('0');
      inc(count[d]);
      w[i - 1] := d;
    end;
    check := true;
    i := 0;
    while check and (i <= 9) do
    begin
      check := count[i] = w[i];
      inc(i);
    end;
  end;

var
  x: longint;

begin
  writeln ('Autodescriptive numbers from 1 to 100000000:');
  for x := 1 to 100000000 do
    if check(x) then
      writeln (' ', x);
  writeln('Job done.');
end.


  

You may also check:How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Nim programming language
You may also check:How to resolve the algorithm S-expressions step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the Logo programming language
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Burrows–Wheeler transform step by step in the Java programming language