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

Published on 12 May 2024 09:40 PM

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

Source code in the livecode programming language

function selfDescNumber n
    local tSelfD, tLen
    put len(n) into tLen
    repeat with x = 0 to (tLen - 1)
        put n into nCopy
        replace x with empty in nCopy 
        put char (x + 1) of n = (tLen - len(nCopy)) into tSelfD
        if not tSelfD then exit repeat
    end repeat
    return tSelfD
end selfDescNumber

on mouseUp
    repeat with n = 0 to 10000000
        if selfDescNumber(n) then
            put n into selfNum[n]
        end if
    end repeat
    combine selfNum using comma
    put selfNum
end mouseUp

1210,2020,21200,3211000

  

You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the OCaml programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Factor programming language
You may also check:How to resolve the algorithm Table creation/Postal addresses step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the SASL programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the FutureBasic programming language