Fraction reduction: Difference between revisions

Content added Content deleted
Line 1,370: Line 1,370:


hasUniqueDigits :: Integral a => a -> Bool
hasUniqueDigits :: Integral a => a -> Bool
hasUniqueDigits n = dl == ul
hasUniqueDigits n = length ds == length ul
where
where
ds = digits 10 n
ds = digits 10 n
dl = length ds
ul = nub ds
ul = length $ nub ds


hasZeros :: Integral a => a -> Bool
hasZeros :: Integral a => a -> Bool
Line 1,412: Line 1,411:
displayResult :: (Fraction, Fraction, Int) -> IO ()
displayResult :: (Fraction, Fraction, Int) -> IO ()
displayResult ((n1,d1),(n2,d2),d) = printf "%d/%d = %d/%d by dropping %d\n" n1 d1 n2 d2 d
displayResult ((n1,d1),(n2,d2),d) = printf "%d/%d = %d/%d by dropping %d\n" n1 d1 n2 d2 d

countReductions :: [(Fraction, Fraction, Int)] -> [(Int, Int)]
countReductions = fmap ((,) . length <*> head) . group . sort . fmap (\(_, _, x) -> x)


displayCount :: [(Fraction, Fraction, Int)] -> Int -> IO ()
displayCount :: [(Fraction, Fraction, Int)] -> Int -> IO ()
Line 1,420: Line 1,416:
printf "There are %d %d-digit fractions of which:\n" (length xs) n
printf "There are %d %d-digit fractions of which:\n" (length xs) n
mapM_ (uncurry (printf "%5d have %d's omitted\n")) (countReductions xs) >> printf "\n"
mapM_ (uncurry (printf "%5d have %d's omitted\n")) (countReductions xs) >> printf "\n"
where
countReductions = fmap ((,) . length <*> head) . group . sort . fmap (\(_, _, x) -> x)


main :: IO ()
main :: IO ()