Fraction reduction: Difference between revisions

m
m (→‎{{header|Haskell}}: performance optimized)
Line 1,359:
type Fraction = (Int, Int)
 
validIntegers :: [Int] -> [Int]
validIntegers xs = [x | x <- xs, not $ hasZeros x, hasUniqueDigits x]
[x | x <- xs
, not (hasZeros x)
, hasUniqueDigits x
]
 
possibleFractions :: [Int] -> [Fraction]
possibleFractions xs =
[(n,d) | n <- validIntegers xs
, d <- validIntegers xs
, n < d
, gcd n d /= 1
, hasCommonDigits (n, d)
]
 
hasUniqueDigits :: Integral a => a -> Bool
Line 1,383 ⟶ 1,370:
hasZeros :: Integral a => a -> Bool
hasZeros = elem 0 . digits 10
 
possibleFractions :: [Int] -> [Fraction]
possibleFractions xs =
[(n,d) | n <- validIntegers xs
, d <- validIntegers xs
, n < d
, gcd n d /= 1
]
 
commonDigits :: Fraction -> [Int]
commonDigits (n1, n2) = digits 10 n1 `intersect` digits 10 n2
 
hasCommonDigits :: Fraction -> Bool
hasCommonDigits = not . null . commonDigits
 
dropDigit :: Integral a => a -> a -> a
Line 1,432 ⟶ 1,424:
groups = [ findReductions [10^1..99]
, findReductions [10^2..999]
, findReductions [10^3..9999]
, findReductions [10^4..99999] ]</lang>
{{out}}
Anonymous user