Self-describing numbers
There are several integers numbers called "self-describing".
Integers with 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.
Position "0" has value 2 and there is two 0 in the number. Position "1" has value 0 because there are not 1's in the number. Position "2" has value 2 and there is two 2. And the position "3" has value 0 and there are zero 3's.
Self-describing numbers < 100.000.000: 1210 - 2020 - 21200 - 3211000 - 42101000
- Task Description
- Write a function/routine/method/... that will check whether a given positive integer is self-describing.
- As an optional stretch goal - generate and display a few self-describing numbers.
BASIC
<lang qbasic>Dim x, r, b, c, n, m As Integer Dim a, d As String Dim v(10), w(10) As Integer Cls For x = 1 To 5000000
a$ = ltrim$(Str$(x)) b = Len(a$) For c = 1 To b d$ = Mid$(a$, c, 1) v(Val(d$)) = v(Val(d$)) + 1 w(c - 1) = Val(d$) Next c r = 0 For n = 0 To 10 If v(n) = w(n) Then r = r + 1 v(n) = 0 w(n) = 0 Next n If r = 11 Then Print x; " Yes,is autodescriptive number"
Next x Print Print "End" sleep end</lang>
D
<lang d>import std.algorithm, std.range, std.conv ; import std.stdio ;
bool selfDescrib(int n) {
auto numb = map!"a - '0'"(to!string(n)) ; auto freq = map!((a){ return count(numb, a) ; })(iota(walkLength(numb))) ;
return equal(numb, freq) ;
}
void main() {
foreach(i ; filter!selfDescrib(iota(100_000_000))) writeln(i) ;
}</lang> Output:
1210 2020 21200 3211000 42101000
J
<lang j> NB. background material:
digits=: 10 #.inv ] digits 2020
2 0 2 0
(,~ i.@#@digits)2020
0 1 2 3 2020
(digits ,~ i.@#@digits)2020
0 1 2 3 2 0 2 0
(,~ i.@#)&digits 2020
0 1 2 3 2 0 2 0
_1 + #/.~@(,~ i.@#)&digits 2020
2 0 2 0
NB. task item 1:
(digits -: _1 + #/.~@(,~ i.@#)&digits) 2020
1
(digits -: _1 + #/.~@(,~ i.@#)&digits) 1210
1
(digits -: _1 + #/.~@(,~ i.@#)&digits) 21200
1
(digits -: _1 + #/.~@(,~ i.@#)&digits) 3211000
1
(digits -: _1 + #/.~@(,~ i.@#)&digits) 43101000
0
(digits -: _1 + #/.~@(,~ i.@#)&digits) 42101000
1
NB. task item 2:
I. (digits -: _1 + #/.~@(,~ i.@#)&digits)"0 i.1e6 NB. task item 2
1210 2020 21200</lang>
Logo
<lang logo>TO XX BT MAKE "AA (ARRAY 10 0) MAKE "BB (ARRAY 10 0) FOR [Z 0 9][SETITEM :Z :AA "0 SETITEM :Z :BB "0 ]
FOR [A 1 50000][ MAKE "B COUNT :A MAKE "Y 0 MAKE "X 0 MAKE "R 0 MAKE "J 0 MAKE "K 0
FOR [C 1 :B][MAKE "D ITEM :C :A SETITEM :C - 1 :AA :D MAKE "X ITEM :D :BB MAKE "Y :X + 1 SETITEM :D :BB :Y MAKE "R 0] FOR [Z 0 9][MAKE "J ITEM :Z :AA MAKE "K ITEM :Z :BB IF :J = :K [MAKE "R :R + 1]]
IF :R = 10 [PR :A] FOR [Z 0 9][SETITEM :Z :AA "0 SETITEM :Z :BB "0 ]] PR [END] END</lang>
PicoLisp
<lang PicoLisp>(de selfDescribing (N)
(not (find '((D I) (<> D (cnt = N (circ I)))) (setq N (mapcar format (chop N))) (range 0 (length N)) ) ) )</lang>
Output:
: (filter selfDescribing (range 1 4000000)) -> (1210 2020 21200 3211000)
Python
<lang python>>>> def isSelfDescribing(n): s = str(n) return all(s.count(str(i)) == int(ch) for i, ch in enumerate(s))
>>> [x for x in range(4000000) if isSelfDescribing(x)] [1210, 2020, 21200, 3211000] >>> [(x, isSelfDescribing(x)) for x in (1210, 2020, 21200, 3211000, 42101000, 521001000, 6210001000)] [(1210, True), (2020, True), (21200, True), (3211000, True), (42101000, True), (521001000, True), (6210001000, True)]</lang>
Tcl
<lang tcl>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}
}</lang>