Fraction reduction: Difference between revisions

Content added Content deleted
Line 1,358: Line 1,358:


type Fraction = (Int, Int)
type Fraction = (Int, Int)
type Reduction = (Fraction, Fraction, Int)


validIntegers :: [Int] -> [Int]
validIntegers :: [Int] -> [Int]
Line 1,391: Line 1,392:
digitsToIntegral = sum . zipWith (*) (iterate (*10) 1)
digitsToIntegral = sum . zipWith (*) (iterate (*10) 1)


findReduction :: Fraction -> [(Fraction, Fraction, Int)]
findReduction :: Fraction -> [Reduction]
findReduction z@(n1, d1) = go $ commonDigits z
findReduction z@(n1, d1) = go $ commonDigits z
where
where
Line 1,404: Line 1,405:
decimalWithDrop = realToFrac n2 / realToFrac d2
decimalWithDrop = realToFrac n2 / realToFrac d2


findReductions :: [Int] -> [(Fraction, Fraction, Int)]
findReductions :: [Int] -> [Reduction]
findReductions = (findReduction =<<) . possibleFractions
findReductions = (findReduction =<<) . possibleFractions


displayResult :: (Fraction, Fraction, Int) -> IO ()
displayResult :: Reduction -> 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


displayCount :: [(Fraction, Fraction, Int)] -> Int -> IO ()
displayCount :: [Reduction] -> Int -> IO ()
displayCount xs n = do
displayCount xs n = do
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