Haskell 中的毕达哥拉斯三元组没有对称解
我必须在 Haskell 中做毕达哥拉斯三元组,而无需对称解。我的尝试是:
terna :: Int -> [(Int,Int,Int)]
terna x = [(a,b,c)|a<-[1..x], b<-[1..x], c<-[1..x], (a^2)+(b^2) == (c^2)]
我得到的结果是:
Main> terna 10
[(3,4,5),(4,3,5),(6,8,10),(8,6,10)]
正如你所看到的,我得到了对称的解决方案,例如:(3,4,5) (4,3,5)。我需要摆脱它们,但我不知道如何做。谁能帮助我吗?
I gotta do the Pythagorean triple in Haskell without symmetrical solutions. My try is:
terna :: Int -> [(Int,Int,Int)]
terna x = [(a,b,c)|a<-[1..x], b<-[1..x], c<-[1..x], (a^2)+(b^2) == (c^2)]
and I get as a result:
Main> terna 10
[(3,4,5),(4,3,5),(6,8,10),(8,6,10)]
As you can see, I´m getting symmetrical solutions like: (3,4,5) (4,3,5). I need to get rid of them but I don´t know how. Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
每次有一个副本时,都会有一个版本,其中 a 大于 b,而另一个版本中 b 大于 a。因此,如果您想确保只获得其中一个,则只需确保
a
始终等于或小于b
,反之亦然。实现此目的的一种方法是将其作为条件添加到列表理解中。
另一种更有效的方法是将
b
的生成器更改为b <- [1..a]
,因此它只生成b 的值小于或等于
a
。说到效率:根本不需要迭代
c
。获得a
和b
的值后,您可以简单地计算(a^2)+(b^2)
并检查它是否有自然平方根。Every time you have a duplicate you have one version in which a is greater than b and one where b is greater than a. So if you want to make sure you only ever get one of them, you just need to make sure that either
a
is always equal to or less thanb
or vice versa.One way to achieve this would be to add it as a condition to the list comprehension.
Another, more efficient way, would be to change
b
's generator tob <- [1..a]
, so it only generates values forb
which are smaller or equal toa
.Speaking of efficiency: There is no need to iterate over
c
at all. Once you have values fora
andb
, you could simply calculate(a^2)+(b^2)
and check whether it has a natural square root.根本不了解 Haskell(也许你现在正在学习它?),但如果你只能采用
a
小于或等于的那些,你似乎可以摆脱它们b
。这将消除重复项。Don't know Haskell at all (perhaps you're learning it now?) but it seems like you could get rid of them if you could take only the ones for which
a
is less than or equal tob
. That would get rid of the duplicates.尝试使用简单的递归生成器:
http://en.wikipedia.org/wiki/Formulas_for_generate_Pythagorean_triples
(新文章)
http://en.wikipedia.org/wiki/Tree_of_primitive_Pythagorean_triples
编辑 (2014 年 5 月 7 日)
在这里,我制作了无限生成器,可以生成按周长排序的原始三元组(但可以修改为按其他参数排序 -斜边,面积,...),只要它认为任何三元组都小于根据提供的比较函数从生成矩阵生成的任何三元组
Try with a simple recursive generator:
http://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples
(new article)
http://en.wikipedia.org/wiki/Tree_of_primitive_Pythagorean_triples
EDIT (7 May 2014)
Here I have made infinite generator that can generate primitive triplets ordered by perimeter (but can be modified to be ordered by other parameter - hypotenuses, area, ...) as long as it holds that any triplet is smaller that any generated from generator matrix according to provided compare function
您可以执行以下操作:
You can do the following:
这可能有效:从本教程中得到它
This might work: Got it from this tutorial
列表理解语法使这变得简单:
这基本上是说我们从
a
s、b
s 和c
s 构建一个列表,其中a
从1
更改为d
,b
从当前的a
更改为d< /code> 等。它还表示
a^2 + b^2 == c^2
应该成立。List comprehension syntax makes this easy:
This basically says than we build a list from
a
s,b
s andc
s, wherea
changes from1
tod
,b
changes from currenta
tod
and etc. It also says thata^2 + b^2 == c^2
should hold.