Self-describing numbers: Difference between revisions
m
→{{header|Haskell}}
m (→{{header|Quackery}}: formatting) |
|||
Line 1,279:
Here are functions for generating all the self-describing numbers of a certain length. We capitalize on the fact (from Wikipedia) that a self-describing number of length n is a base-n number (i.e. all digits are 0..n-1).
<lang haskell>import
import
count :: Int -> [Int] -> Int
count x = length . filter (x ==)
--
--
-- one per digit
allBaseNNumsOfLength :: Int -> [[Int]]
allBaseNNumsOfLength =
replicateM
<*> (enumFromTo 0 . subtract 1)
isSelfDescribing :: [Int] -> Bool
isSelfDescribing num =
all (\(i, x) -> x == count i num) $
zip [0 ..] num
--
decimalize :: [Int] -> Int
decimalize = read . map intToDigit
Line 1,300 ⟶ 1,305:
main =
(print . concat) $
map decimalize
. filter isSelfDescribing
. allBaseNNumsOfLength
<$> [1 .. 8]</lang>
{{Out}}
<pre>[1210,2020,21200,3211000,42101000]</pre>
|