Last active
December 10, 2015 06:28
-
-
Save gnarmis/4394579 to your computer and use it in GitHub Desktop.
Gardner's canceling digits puzzle, also related to Project Euler problem #33
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- turn an Integer into a list of digits | |
digits = map (read . (:[])) . show | |
-- is a given fraction special? | |
special :: Integer -> Integer -> Bool | |
special n d | |
| (n_rf / d_rf) == ((realToFrac (head ns)) / (realToFrac (head ds))) = True | |
| otherwise = False | |
where n_rf = realToFrac n | |
d_rf = realToFrac d | |
(ns, ds) = (cancel_digits (digits n) (digits d)) | |
cancel_digits :: (Integral t) => [t] -> [t] -> ([t], [t]) | |
cancel_digits num_digs den_digs | |
| (null ns) && (null ds) = ([1],[1]) | |
| (null ns) && not (null ds) = ([1],ds) | |
| not (null ns) && (null ds) = (ns, [1]) | |
| otherwise = (ns,ds) | |
where ns = [n | n <- num_digs, not (n `elem` den_digs)] | |
ds = [d | d <- den_digs, not (d `elem` num_digs)] | |
allCombosUnder100 :: [(Integer, Integer)] | |
allCombosUnder100 = [(x,y) | x <- [1..100], y <- [1..100]] | |
specialCombos :: [(Integer, Integer)] | |
specialCombos = filter (\t -> (special (fromIntegral (fst t)) | |
(fromIntegral (snd t)))) | |
allCombosUnder100 | |
specialNonTrivial :: [(Integer, Integer)] | |
specialNonTrivial = | |
filter (\t -> (nonTrivial (orig t) (cancelled t))) specialCombos | |
where orig t = ((digits (fst t)), (digits (snd t))) | |
cancelled t = cancel_digits (digits (fst t)) (digits (snd t)) | |
nonTrivial orig cancelled = | |
not_same && did_cancel && no_zeros | |
where not_same = (fst cancelled) /= (snd cancelled) | |
did_cancel = (length (fst orig)) > (length (fst cancelled)) | |
no_zeros = not $ foldl (&&) True (map (elem 0) [(fst orig),(snd orig)]) |
Lol yeah, I've been lookin around for stuff to do in Haskell too
answer: specialNonTrivial
(not for project euler, just for the 'weird numbers')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
haha I cant figure out anything to code in haskell to play around with it. good call :p