tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,16 @@
|
|||
import Data.Char
|
||||
|
||||
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)]
|
||||
|
||||
main = do
|
||||
let tests = [1210, 2020, 21200, 3211000,
|
||||
42101000, 521001000, 6210001000]
|
||||
print $ map isSelfDescribing tests
|
||||
print $ filter isSelfDescribing [0 .. 4000000]
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
import Data.Char (intToDigit)
|
||||
import Control.Monad (replicateM, forM_)
|
||||
|
||||
count :: Int -> [Int] -> Int
|
||||
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]
|
||||
|
||||
isSelfDescribing :: [Int] -> Bool
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue