Self-describing numbers: Difference between revisions

Content added Content deleted
(Red version)
Line 1,835: Line 1,835:


Sadly, the implementation is too slow for the optional task, taking somewhere around 3 minutes to check all numbers below 100.000.000
Sadly, the implementation is too slow for the optional task, taking somewhere around 3 minutes to check all numbers below 100.000.000
=={{header|Red}}==
<lang Red>Red []


;;-------------------------------------
count-dig: func ["count occurence of digit in number"
;;-------------------------------------
s [string!] "number as string"
sdig [char!] "search number as char"
][
cnt: #"0" ;; counter as char for performance optimization

while [s: find/tail s sdig][cnt: cnt + 1]
return cnt
]

;;-------------------------------------
isSDN?: func ["test if number is self describing number"
s [string!] "number to test as string "
][
;;-------------------------------------

ind: #"0" ;; use digit as char for performance optimization

foreach ele s [
if ele <> count-dig s ind [return false]
ind: ind + 1
]
return true
]

repeat i 4000000 [ if isSDN? to-string i [print i] ]
</lang>
'''output''
<pre>1210
2020
21200
3211000
>>
</pre>
=={{header|REXX}}==
=={{header|REXX}}==
Also see: &nbsp; [http://oeis.org/A046043 OEIS A46043]
Also see: &nbsp; [http://oeis.org/A046043 OEIS A46043]