Pythagorean triples: Difference between revisions

Content added Content deleted
(Added Kotlin)
(→‎{{header|Haskell}}: Constrained the (primitive generation) test to terminate (at 10^7), + hlint, hindent)
Line 1,706: Line 1,706:
Recursive primitive generation:
Recursive primitive generation:
<lang haskell>triangles :: Int -> [[Int]]
<lang haskell>triangles :: Int -> [[Int]]
triangles max_peri | max_peri < 12 = []
triangles max_peri
| max_peri < 12 = []
| otherwise = concat tiers where
| otherwise = concat tiers
tiers = takeWhile (not.null) $ iterate tier [[3,4,5]]
where
tier = concatMap (filter ((<=max_peri).sum).tmul)
tiers = takeWhile (not . null) $ iterate tier [[3, 4, 5]]
tmul t = map (map (sum . zipWith (*) t))
tier = concatMap (filter ((<= max_peri) . sum) . tmul)
[[[ 1,-2,2],[ 2,-1,2],[ 2,-2,3]],
tmul t =
[[ 1, 2,2],[ 2, 1,2],[ 2, 2,3]],
map
[[-1, 2,2],[-2, 1,2],[-2, 2,3]]]
(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)
triangleCount max_p = (length t, sum $ map ((max_p `div`) . sum) t)
where t = triangles max_p
where
t = triangles max_p


main = mapM_ putStrLn $
main :: IO ()
main =
map (\n -> show n ++ " " ++ show (triangle_count n)) $ map (10^) [1..]</lang>
mapM_
((putStrLn . (\n -> show n ++ " " ++ show (triangleCount n))) . (10 ^))
[1 .. 7]</lang>
{{out}}
{{out}}
<pre>
<pre>10 (0,0)
10 (0,0)
100 (7,17)
100 (7,17)
1000 (70,325)
1000 (70,325)
Line 1,728: Line 1,736:
100000 (7026,64741)
100000 (7026,64741)
1000000 (70229,808950)
1000000 (70229,808950)
10000000 (702309,9706567)
10000000 (702309,9706567)</pre>
...
</pre>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==