Jump to content

Fraction reduction: Difference between revisions

m
Line 1,377:
, d <- validIntegers xs
, n < d
, gcd n d /= 1 ]
]
 
commonDigits :: Fraction -> [Int]
Line 1,393 ⟶ 1,392:
 
findReduction :: Fraction -> [Reduction]
findReduction z@(n1, d1) = gofoldr f [] $ commonDigits z
where
decimal = realToFrac n1 / realToFrac d1
f x r | decimalWithDrop == decimal = (z, (n2, d2), x) : go xsr
go [] = []
| otherwise = go xsr
go (x:xs)
| decimalWithDrop == decimal = (z, (n2, d2), x) : go xs
| otherwise = go xs
where
n2 = dropDigit x n1
Line 1,423 ⟶ 1,420:
mapM_ (uncurry displayCount) $ zip groups [2..]
where
groups = [ findReductions [10^1..99], findReductions [10^2..999]
, findReductions [10^23..9999999], findReductions [10^4..99999] ]</lang>
, findReductions [10^3..9999]
, findReductions [10^4..99999] ]</lang>
{{out}}
<pre>16/64 = 1/4 by dropping 6
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.