Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,17 @@
import Data.Char
count :: Int -> [Int] -> Int
count x = length . filter (x ==)
isSelfDescribing :: Integer -> Bool
isSelfDescribing n = nu == f
where
nu = digitToInt <$> show n
f = (`count` nu) <$> [0 .. length nu - 1]
main :: IO ()
main = do
print $
isSelfDescribing <$>
[1210, 2020, 21200, 3211000, 42101000, 521001000, 6210001000]
print $ filter isSelfDescribing [0 .. 4000000]

View file

@ -0,0 +1,30 @@
import Control.Monad (forM_, replicateM)
import Data.Char (intToDigit)
count :: Int -> [Int] -> Int
count x = length . filter (x ==)
-- All the combinations of n digits of base n.
-- A base-n number is represented as a list of ints,
-- 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
-- Translated back into an integer in base-10
decimalize :: [Int] -> Int
decimalize = read . map intToDigit
main :: IO ()
main =
(print . concat) $
map decimalize
. filter isSelfDescribing
. allBaseNNumsOfLength
<$> [1 .. 8]