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
]
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."
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 :: Int -> [[Int]]
pythagoreanTriplesBelow n =
let m = quot n 2▼
(\y -> concatMap (go x y) [y + 1 .. m])
[x + 1
)
▲ concatMap
[1 ..
where
go x y z
| x + y + z <= n && x ^ 2 +
|
main :: IO ()
main =
mapM_
(print . length)
( [id, filter (\[x, y, _] -> gcd x y == 1)]
<*> [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]],
]
Line 2,298 ⟶ 2,304:
main =
mapM_
((putStrLn . (\n -> show n
[1 .. 7]</lang>
{{out}}
|