Pythagorean triples: Difference between revisions
Content added Content deleted
m (→using single evenness for determinacy: added and changed some comments, changed some REXX variable names.) |
(→{{header|Haskell}}: Reformatting an existing contribution for slightly more legibility) |
||
Line 1,678: | Line 1,678: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell>pytr |
<lang haskell>pytr :: Int -> [(Bool, Int, Int, Int)] |
||
pytr n = |
|||
⚫ | |||
filter |
|||
(\(_, a, b, c) -> a + b + c <= n) |
|||
[ (prim a b c, a, b, c) |
|||
| a <- [1 .. n] |
|||
, b <- [1 .. n] |
|||
, c <- [1 .. n] |
|||
, a < b && b < c |
|||
, a ^ 2 + b ^ 2 == c ^ 2 ] |
|||
where |
|||
⚫ | |||
main :: IO () |
|||
main = putStrLn $ "Up to 100 there are " ++ (show $ length xs) ++ " triples, of which " ++ (show $ length $ filter (\(x,_,_,_) -> x == True) xs) ++ " are primitive." |
|||
main = |
|||
⚫ | |||
putStrLn $ |
|||
</lang> |
|||
"Up to 100 there are " ++ |
|||
show (length xs) ++ |
|||
" triples, of which " ++ |
|||
show (length $ filter (\(x, _, _, _) -> x) xs) ++ " are primitive." |
|||
where |
|||
⚫ | |||
{{Out}} |
|||
<pre>Up to 100 there are 17 triples, of which 7 are primitive.</pre> |
<pre>Up to 100 there are 17 triples, of which 7 are primitive.</pre> |
||