Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@gnarmis /gardner.hs
Last active

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Gardner's canceling digits puzzle, also related to Project Euler problem #33
-- 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))))
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)])

haha I cant figure out anything to code in haskell to play around with it. good call :p


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
Something went wrong with that request. Please try again.