September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,5 +1,22 @@
|
|||
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
|
||||
pytr :: Int -> [(Bool, Int, Int, Int)]
|
||||
pytr n =
|
||||
filter
|
||||
(\(_, a, b, c) -> a + b + c <= n)
|
||||
[ (prim a b c, a, b, c)
|
||||
| a <- xs
|
||||
, b <- drop a xs
|
||||
, c <- drop b xs
|
||||
, a ^ 2 + b ^ 2 == c ^ 2 ]
|
||||
where
|
||||
xs = [1 .. n]
|
||||
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
|
||||
main :: IO ()
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,15 +1,22 @@
|
|||
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]]]
|
||||
pythagoreanTriplesBelow :: Int -> [[Int]]
|
||||
pythagoreanTriplesBelow n =
|
||||
let m = quot n 2
|
||||
in concatMap
|
||||
(\x ->
|
||||
concatMap
|
||||
(\y ->
|
||||
concatMap
|
||||
(\z ->
|
||||
if x + y + z <= n && x ^ 2 + y ^ 2 == z ^ 2
|
||||
then [[x, y, z]]
|
||||
else [])
|
||||
[y + 1 .. m])
|
||||
[x + 1 .. m])
|
||||
[1 .. m]
|
||||
|
||||
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..]
|
||||
-- TEST -------------------------------------------------------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
(print . length)
|
||||
([id, filter (\[x, y, _] -> gcd x y == 1)] <*> [pythagoreanTriplesBelow 100])
|
||||
|
|
|
|||
24
Task/Pythagorean-triples/Haskell/pythagorean-triples-3.hs
Normal file
24
Task/Pythagorean-triples/Haskell/pythagorean-triples-3.hs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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]]
|
||||
]
|
||||
|
||||
triangleCount max_p = (length t, sum $ map ((max_p `div`) . sum) t)
|
||||
where
|
||||
t = triangles max_p
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
((putStrLn . (\n -> show n ++ " " ++ show (triangleCount n))) . (10 ^))
|
||||
[1 .. 7]
|
||||
Loading…
Add table
Add a link
Reference in a new issue