Jump to content

Pythagorean triples: Difference between revisions

→‎{{header|Haskell}}: Reformatting an existing contribution for slightly more legibility
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:
=={{header|Haskell}}==
 
<lang haskell>pytr n:: = filter (\(_, a, b, c)Int -> a+b+c <= n) [(prim a b cBool, aInt, bInt, cInt) | a <- [1..n], b <- [1..n], c <- [1..n], a < b && b < c, a^2 + b^2 == c^2]
pytr n =
where prim a b _ = gcd a b == 1
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
where prim a b _ = gcd a b == 1
 
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 =
where xs = pytr 100
putStrLn $
</lang>
"Up to 100 there are " ++
show (length xs) ++
" triples, of which " ++
show (length $ filter (\(x, _, _, _) -> x) xs) ++ " are primitive."
where
where xs = pytr 100</lang>
 
{{Out}}
<pre>Up to 100 there are 17 triples, of which 7 are primitive.</pre>
 
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.