admin管理员组文章数量:1125772
I came across this question asking me to find all pythagorean triples of natural numbers, such that all 3 numbers are co-primes, in ascending order of c.
I came up with this solution, using a function to filter out the multiples and then a list comprehension.
sieve :: [Int] -> [Int]
sieve (x:xs) = filter (multiple x) xs
where multiple a b = b `mod` a == 0
pythagoreanTriples :: [(Int, Int, Int)]
pythagoreanTriples = [(a,b,c) | c <- [1..], b <- sieve (c : [1..c]), a <- sieve (c:[1..c]), a*a + b*b == c*c]
The answer in the model answers was much more complicated, including trying to find the remainder of all 3 numbers with the first. Would this still work as intended?
I came across this question asking me to find all pythagorean triples of natural numbers, such that all 3 numbers are co-primes, in ascending order of c.
I came up with this solution, using a function to filter out the multiples and then a list comprehension.
sieve :: [Int] -> [Int]
sieve (x:xs) = filter (multiple x) xs
where multiple a b = b `mod` a == 0
pythagoreanTriples :: [(Int, Int, Int)]
pythagoreanTriples = [(a,b,c) | c <- [1..], b <- sieve (c : [1..c]), a <- sieve (c:[1..c]), a*a + b*b == c*c]
The answer in the model answers was much more complicated, including trying to find the remainder of all 3 numbers with the first. Would this still work as intended?
Share Improve this question asked 2 days ago internationalmysterymaninternationalmysteryman 111 bronze badge New contributor internationalmysteryman is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1 |1 Answer
Reset to default 0The code never returns a solution, partly because the sieve
has been implemented the wrong way. Indeed, if you want two items to be co-prime, the sieve should thus retain all items that do not share another factor except 1
. For example:
sieve :: Int -> [Int] -> [Int]
sieve x xs = filter (coprime x) xs
where coprime x y = gcd x y == 1
then we can work with:
pythagoreanTriples :: [(Int, Int, Int)]
pythagoreanTriples = [(a,b,c) | c <- [1..], let bs = sieve c [2 .. c], b <- bs, a <- sieve b bs, a*a + b*b == c*c]
But here the sieve probably will only increase the work, why not just use:
pythagoreanTriples :: [(Int, Int, Int)]
pythagoreanTriples = [
(a,b,c)
| c <- [2..]
, b <- [2..c-1]
, gcd b c == 1
, a <- [2..c-1]
, gcd a b == 1
, gcd a c == 1
, a*a + b*b == c*c
]
or if we want unique triples (i.e. not swapping the values of a
and b
), we can use:
pythagoreanTriples :: [(Int, Int, Int)]
pythagoreanTriples = [
(a,b,c)
| c <- [2..]
, b <- [2..c-1]
, gcd b c == 1
, a <- [2..b-1]
, gcd a b == 1
, gcd a c == 1
, a*a + b*b == c*c
]
本文标签: mathWould this be a suitable way to find pythagorean triples in haskellStack Overflow
版权声明:本文标题:math - Would this be a suitable way to find pythagorean triples in haskell? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736638275a1945932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
a
andb
here are not guaranteed to be coprime, onlya
andc
are. But thesieve
also does not check if numbers are co-prime, it does the opposite, and only retains multiples ofx
. – willeM_ Van Onsem Commented 2 days ago