Pythagorean triples: Difference between revisions

m
→‎{{header|Haskell}}: Applied Ormolu, preferred <> to ++
m (→‎using single evenness for determinacy: elided a word from the output section.)
m (→‎{{header|Haskell}}: Applied Ormolu, preferred <> to ++)
Line 2,227:
(\(_, a, b, c) -> a + b + c <= n)
[ (prim a b c, a, b, c)
| a <- xs ,
, b <- drop a xs ,
, c <- drop b xs ,
, a ^ 2 + b ^ 2 == c ^ 2 ]
]
where
xs = [1 .. n]
Line 2,238 ⟶ 2,239:
main =
putStrLn $
"Up to 100 there are " ++
<> show (length xs) ++
<> " triples, of which " ++
<> show (length $ filter (\(x, _, _, _) -> x) xs) ++ " are primitive."
<> " are primitive."
where
xs = pytr 100</lang>
Line 2,249 ⟶ 2,251:
 
Or equivalently (desugaring the list comprehension down to nested concatMaps, and pruning back the search space a little):
<lang haskell>pythagoreanTriplesBelow------------------- ::PYTHAGOREAN IntTRIPLES -> [[Int]]-----------------
 
pythagoreanTriplesBelow :: Int -> [[Int]]
pythagoreanTriplesBelow n =
concatMap
let m = quot n 2
in concatMap ( \x ->
(\x ->concatMap
(\y -> concatMap (go x y) [y + 1 .. m])
[x + 1 (\y.. ->m]
)
concatMap
[1 .. (\z ->m]
where
if x + y + z <= n && x ^ 2 + y ^ 2 == z ^ 2
let m = quot n 2
then [[x, y, z]]
go x y z
else [])
| x + y + z <= n && x ^ 2 + [y +^ 2 == z 1^ ..2 m])=
[[x, +y, 1 .. mz]])
| [1otherwise ..= m[]
 
-- TEST ------------------------------------------------ TEST -------------------------
main :: IO ()
main =
mapM_
(print . length)
( [id, filter (\[x, y, _] -> gcd x y == 1)] <*> [pythagoreanTriplesBelow 100])</lang>
<*> [pythagoreanTriplesBelow 100]
)</lang>
{{Out}}
<pre>17
Line 2,286 ⟶ 2,292:
map
(map (sum . zipWith (*) t))
[ [[1, -2, 2], [2, -1, 2], [2, -2, 3]],
, [[1, 2, 2], [2, 1, 2], [2, 2, 3]],
, [[-1, 2, 2], [-2, 1, 2], [-2, 2, 3]]
]
 
Line 2,298 ⟶ 2,304:
main =
mapM_
((putStrLn . (\n -> show n ++<> " " ++<> show (triangleCount n))) . (10 ^))
[1 .. 7]</lang>
{{out}}
9,659

edits