1
0
Fork 0

Finish up Euler 9

main
Bill Ewanick 2023-09-14 15:06:12 -04:00
parent bd0b941a6a
commit 1b689521c1
1 changed files with 71 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import Debug.Trace (trace)
import Graphics.Rendering.Chart.Backend.Cairo
import Graphics.Rendering.Chart.Easy
{-
@ -22,13 +23,83 @@ answer :: String
answer = "I dunno"
solve :: Integer -> [(Integer, Integer, Integer)]
solve x = takeWhile (\(a,b,c) -> a + b + c <= 1000) $ primitiveTriplesUnder x
euclid'sFormula :: Num c => (c, c) -> (c, c, c)
euclid'sFormula (m, n) = (a,b,c)
where
a = m^2 - n^2
b = 2*m*n
c = m^2 + n^2
listOfMNs :: Integer -> [(Integer, Integer)]
listOfMNs x =
[ (m,n)
| n <- [2,4..x] -- one of them is even
, m <- [n+1,n+3..x]
, gcd m n == 1 -- coprime
]
listOfMNs' :: Integer -> [(Integer, Integer)]
listOfMNs' x =
[ (m,n)
| n <- [2,4..] -- one of them is even
, m <- [n+1,n+3..]
, gcd m n == 1 -- coprime
, a m n + b m n + c m n <= x
] where
a m n = m^2 - n^2
b m n = 2*m*n
c m n = m^2 + n^2
primitiveTriplesUnder :: Integer -> [(Integer, Integer, Integer)]
primitiveTriplesUnder = map euclid'sFormula . listOfMNs
test :: [(Integer, Integer, Integer)]
test = [ (a,b,c)
| a <- [3..],
b <- take 10 [a+1..],
c <- takeWhile (\c -> a^2 + b^2 <= c^2) [b+1..]
]
ls :: [(Integer, Integer)]
ls = filter (\(m,n) -> gcd m n == 1) $ zip [5,9..] [2,4..]
diags :: Integer -> [(Integer, Integer)]
diags n = [(x,y) | x<-[0..n], y<-[0..n]]
graph :: IO ()
graph = toFile def "test.png" $ do
layout_title .= "Points"
plot (line "points" [ [ (x,y) | (x,y) <- listOfMNs 30] ])
-- main'' = print ans'
ans' :: Integer -> [(Integer, Integer, Integer)]
ans' limit = [(a, b, c)
| a <- [1 .. limit]
, b <- [a + 1 .. limit]
, c <- [limit - a - b]
, b < c
]
{-
- Solution to Project Euler problem 9
- Copyright (c) Project Nayuki. All rights reserved.
-
- https://www.nayuki.io/page/project-euler-solutions
- https://github.com/nayuki/Project-Euler-solutions
-}
-- Computers are fast, so we can implement a brute-force search to directly solve the problem.
perim = 1000
main = putStrLn (show ans)
ans = head [a * b * (perim - a - b) | a <- [1..perim], b <- [a+1..perim], isIntegerRightTriangle a b]
isIntegerRightTriangle a b = a < b && b < c
&& a * a + b * b == c * c
where c = perim - a - b