Pythagorean triples: Difference between revisions
Moved Icon into alphabetic order after Haskell
m (→JS ES6: (add comment on empty lists eliminated by concatMap)) |
(Moved Icon into alphabetic order after Haskell) |
||
Line 1,675:
a+b+c <= 10E 9 70230484 1294080089
a+b+c <= 10E10 702304875 14557915466</pre>
=={{header|Haskell}}==▼
<lang haskell>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 prim a b _ = gcd a b == 1▼
main = putStrLn $ "Up to 100 there are " ++ (show $ length xs) ++ " triples, of which " ++ (show $ length $ filter (\(x,_,_,_) -> x == True) xs) ++ " are primitive."▼
where xs = pytr 100▼
</lang>▼
<pre>Up to 100 there are 17 triples, of which 7 are primitive.</pre>▼
Recursive primitive generation:▼
<lang haskell>triangles :: Int -> [[Int]]▼
triangles max_peri | max_peri < 12 = [] ▼
| otherwise = concat tiers where▼
tiers = takeWhile (not.null) $ iterate tier [[3,4,5]]▼
tier = concatMap (filter ((<=max_peri).sum).tmul)▼
tmul t = 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]]]▼
triangle_count max_p = (length t, sum $ map ((max_p `div`).sum) t)▼
where t = triangles max_p▼
main = mapM_ putStrLn $▼
map (\n -> show n ++ " " ++ show (triangle_count n)) $ map (10^) [1..]</lang>▼
{{out}}▼
<pre>▼
10 (0,0)▼
100 (7,17)▼
1000 (70,325)▼
10000 (703,4858)▼
100000 (7026,64741)▼
1000000 (70229,808950)▼
10000000 (702309,9706567)▼
...▼
</pre>▼
=={{header|Icon}} and {{header|Unicon}}==
This uses the elegant formula (#IV) from [[wp:Formulas_for_generating_Pythagorean_triples|Formulas for generating Pythagorean triples]]
Line 1,746 ⟶ 1,786:
Time=560625, Collections: total=16 string=8 block=8</pre>
▲=={{header|Haskell}}==
▲<lang haskell>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 prim a b _ = gcd a b == 1
▲main = putStrLn $ "Up to 100 there are " ++ (show $ length xs) ++ " triples, of which " ++ (show $ length $ filter (\(x,_,_,_) -> x == True) xs) ++ " are primitive."
▲ where xs = pytr 100
▲</lang>
▲<pre>Up to 100 there are 17 triples, of which 7 are primitive.</pre>
▲Recursive primitive generation:
▲<lang haskell>triangles :: Int -> [[Int]]
▲triangles max_peri | max_peri < 12 = []
▲ | otherwise = concat tiers where
▲ tiers = takeWhile (not.null) $ iterate tier [[3,4,5]]
▲ tier = concatMap (filter ((<=max_peri).sum).tmul)
▲ tmul t = 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]]]
▲triangle_count max_p = (length t, sum $ map ((max_p `div`).sum) t)
▲ where t = triangles max_p
▲main = mapM_ putStrLn $
▲ map (\n -> show n ++ " " ++ show (triangle_count n)) $ map (10^) [1..]</lang>
▲{{out}}
▲<pre>
▲10 (0,0)
▲100 (7,17)
▲1000 (70,325)
▲10000 (703,4858)
▲100000 (7026,64741)
▲1000000 (70229,808950)
▲10000000 (702309,9706567)
▲...
▲</pre>
=={{header|J}}==
|