June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -4,13 +4,14 @@ count :: Int -> [Int] -> Int
|
|||
count x = length . filter (x ==)
|
||||
|
||||
isSelfDescribing :: Integer -> Bool
|
||||
isSelfDescribing n =
|
||||
nu == f where
|
||||
nu = map digitToInt (show n)
|
||||
f = map (\a -> count a nu) [0 .. ((length nu)-1)]
|
||||
isSelfDescribing n = nu == f
|
||||
where
|
||||
nu = digitToInt <$> show n
|
||||
f = (`count` nu) <$> [0 .. length nu - 1]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let tests = [1210, 2020, 21200, 3211000,
|
||||
42101000, 521001000, 6210001000]
|
||||
print $ map isSelfDescribing tests
|
||||
print $ filter isSelfDescribing [0 .. 4000000]
|
||||
print $
|
||||
isSelfDescribing <$>
|
||||
[1210, 2020, 21200, 3211000, 42101000, 521001000, 6210001000]
|
||||
print $ filter isSelfDescribing [0 .. 4000000]
|
||||
|
|
|
|||
|
|
@ -7,15 +7,16 @@ count x = length . filter (x ==)
|
|||
-- all the combinations of n digits of base n
|
||||
-- a base-n number are represented as a list of ints, one per digit
|
||||
allBaseNNumsOfLength :: Int -> [[Int]]
|
||||
allBaseNNumsOfLength n = replicateM n [0..n-1]
|
||||
allBaseNNumsOfLength = replicateM <*> (enumFromTo 0 . subtract 1)
|
||||
|
||||
isSelfDescribing :: [Int] -> Bool
|
||||
isSelfDescribing num =
|
||||
all (\(i,x) -> x == count i num) $ zip [0..] num
|
||||
isSelfDescribing num = all (\(i, x) -> x == count i num) $ zip [0 ..] num
|
||||
|
||||
-- translate it back into an integer in base-10
|
||||
decimalize :: [Int] -> Int
|
||||
decimalize = read . map intToDigit
|
||||
|
||||
main = forM_ [1..7] $
|
||||
print . map decimalize . filter isSelfDescribing . allBaseNNumsOfLength
|
||||
main :: IO ()
|
||||
main =
|
||||
(print . concat) $
|
||||
map decimalize . filter isSelfDescribing . allBaseNNumsOfLength <$> [1 .. 8]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue