Fraction reduction: Difference between revisions

m
→‎{{header|Haskell}}: performance optimized
m (→‎{{header|Haskell}}: performance optimized)
Line 1,353:
</pre>
=={{header|Haskell}}==
<lang haskell>import Control.Monad (guard)
import Data.List (intersect, unfoldr, delete, nub, group, sort)
import Text.Printf (printf)
 
type Fraction = (Int, Int)
 
validIntegers :: [Int] -> [Int]
validIntegers xs =
[x | x <- xs
, not (hasZeros n) && not (hasZeros dx)
, hasUniqueDigits n && hasUniqueDigits d ]x
]
 
possibleFractions :: [Int] -> [Fraction]
possibleFractions xs =
[(n,d) | n <- validIntegers xs
, d <- validIntegers xs
, n < d
, gcd n d /= 1
, not (hasZeros n) && not (hasZeros d)
, hasCommonDigits (n, d)
]
, hasUniqueDigits n && hasUniqueDigits d ]
 
hasUniqueDigits :: Integral a => a -> Bool
Line 1,421 ⟶ 1,427:
main :: IO ()
main = do
mapM_ (\g -> mapM_ displayResult (take 12 g) >> printf "\n") [group1, group2, group3]groups
mapM_ (uncurry displayCount) [(group1,$ 2),zip (group2,groups 3), (group3, 4)[2..]
where
group1groups = [ findReductions [10^1..99]
group2 = , findReductions [10010^2..999]
group3 = , findReductions [100010^3..9999]</lang>
, findReductions [10^4..99999] ]</lang>
{{out}}
<pre>16/64 = 1/4 by dropping 6
Line 1,458 ⟶ 1,465:
1298/3894 = 128/384 by dropping 9
1298/5192 = 128/512 by dropping 9
 
12349/24698 = 1234/2468 by dropping 9
12356/67958 = 1236/6798 by dropping 5
12358/14362 = 1258/1462 by dropping 3
12358/15364 = 1258/1564 by dropping 3
12358/17368 = 1258/1768 by dropping 3
12358/19372 = 1258/1972 by dropping 3
12358/21376 = 1258/2176 by dropping 3
12358/25384 = 1258/2584 by dropping 3
12359/61795 = 1235/6175 by dropping 9
12364/32596 = 1364/3596 by dropping 2
12379/61895 = 1237/6185 by dropping 9
12386/32654 = 1386/3654 by dropping 2
 
There are 4 2-digit fractions of which:
Line 1,481 ⟶ 1,501:
16 have 7's omitted
17 have 8's omitted
390 have 9's omitted</pre>
 
There are 5087 5-digit fractions of which:
75 have 1's omitted
40 have 2's omitted
376 have 3's omitted
78 have 4's omitted
209 have 5's omitted
379 have 6's omitted
591 have 7's omitted
351 have 8's omitted
2988 have 9's omitted</pre>
 
=={{header|J}}==
Anonymous user