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

Published on 12 May 2024 09:40 PM

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

Source code in the sidef programming language

func sdn(Number n) {
    var b = [0]*n.len
    var a = n.digits.flip
    a.each { |i| b[i] := 0 ++ }
    a == b
}

var values = [1210, 2020, 21200, 3211000,
42101000, 521001000, 6210001000, 27, 115508]

values.each { |test|
    say "#{test} is #{sdn(test) ? '' : 'NOT ' }a self describing number."
}

say "\nSelf-descriptive numbers less than 1e5 (in base 10):"
^1e5 -> each { |i| say i if sdn(i) }


for b in (7 .. 36) {
    var n = ((b-4) * b**(b-1) + 2*(b**(b-2)) + b**(b-3) + b**3 -> base(b))
    say "base #{'%2d' % b}: #{n}"
}


  

You may also check:How to resolve the algorithm Bitmap step by step in the Forth programming language
You may also check:How to resolve the algorithm Pancake numbers step by step in the Phix programming language
You may also check:How to resolve the algorithm Even or odd step by step in the EMal programming language
You may also check:How to resolve the algorithm Program name step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the C++ programming language