Fraction reduction: Difference between revisions

Content added Content deleted
Line 1,377: Line 1,377:
, d <- validIntegers xs
, d <- validIntegers xs
, n < d
, n < d
, gcd n d /= 1
, gcd n d /= 1 ]
]


commonDigits :: Fraction -> [Int]
commonDigits :: Fraction -> [Int]
Line 1,393: Line 1,392:


findReduction :: Fraction -> [Reduction]
findReduction :: Fraction -> [Reduction]
findReduction z@(n1, d1) = go $ commonDigits z
findReduction z@(n1, d1) = foldr f [] $ commonDigits z
where
where
decimal = realToFrac n1 / realToFrac d1
decimal = realToFrac n1 / realToFrac d1
f x r | decimalWithDrop == decimal = (z, (n2, d2), x) : r
go [] = []
| otherwise = r
go (x:xs)
| decimalWithDrop == decimal = (z, (n2, d2), x) : go xs
| otherwise = go xs
where
where
n2 = dropDigit x n1
n2 = dropDigit x n1
Line 1,423: Line 1,420:
mapM_ (uncurry displayCount) $ zip groups [2..]
mapM_ (uncurry displayCount) $ zip groups [2..]
where
where
groups = [ findReductions [10^1..99]
groups = [ findReductions [10^1..99], findReductions [10^2..999]
, findReductions [10^2..999]
, findReductions [10^3..9999], findReductions [10^4..99999] ]</lang>
, findReductions [10^3..9999]
, findReductions [10^4..99999] ]</lang>
{{out}}
{{out}}
<pre>16/64 = 1/4 by dropping 6
<pre>16/64 = 1/4 by dropping 6