Reputation: 6107
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^(2) + b^(2) = c^(2). There exists exactly one Pythagorean triplet for which a + b + c = 1000.
I am trying to generate a list with the answer:
[200,375,425]
For that, I programmed in Haskell:
p = [ [a, b, c] | b <- [1..1000], a <- [1..b], let c = 1000 - b - a, a^2 + b^2 == c^2 && b<c]
This generates
[[200,375,425]]
How can I change my solution to generate the first output (instead of the one I have now)?
Upvotes: 1
Views: 2014
Reputation: 3217
Look at your output. It generated a list whose first result is the result you want. There's a function that takes a list and returns the first element -- head
. Call head
on the output from your existing code, and you will get the result you want.
Upvotes: 4
Reputation: 23812
head $ [ [a, b, c] | b <- [1..1000], a <- [1..b], let c = 1000 - b - a, a^2 + b^2 == c^2 && b<c]
Upvotes: 3