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

Published on 12 May 2024 09:40 PM

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

Source code in the tcl programming language

package require Tcl 8.5
proc isSelfDescribing num {
    set digits [split $num ""]
    set len [llength $digits]
    set count [lrepeat $len 0]
    foreach d $digits {
	if {$d >= $len} {return false}
	lset count $d [expr {[lindex $count $d] + 1}]
    }
    foreach d $digits c $count {if {$c != $d} {return false}}
    return true
}

for {set i 0} {$i < 100000000} {incr i} {
    if {[isSelfDescribing $i]} {puts $i}
}


  

You may also check:How to resolve the algorithm Hamming numbers step by step in the Clojure programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the D programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the Ada programming language
You may also check:How to resolve the algorithm Comments step by step in the Vorpal programming language
You may also check:How to resolve the algorithm Gray code step by step in the Racket programming language