您如何找到所有数字的列表,这些数字是2、3和5的幂的倍数? [英] How do you find the list of all numbers that are multiples of only powers of 2, 3, and 5?
问题描述
我正在尝试生成所有可以用,其中a,b和c是整数.我尝试了以下方法,
I am trying to generate a list of all multiples which can be represented by the form , where a, b, and c are whole numbers. I tried the following,
[ a * b * c | a <- map (2^) [0..], b <- map (3^) [0..], c <- map (5^) [0..] ]
但它仅列出5的幂,而从不继续增加2或3.
but it only lists powers of 5 and never goes on to 2 or 3.
对不起,似乎我没有足够清楚地阐明这个问题.我想要的是一个有序的无限列表,尽管我可以对一个有限列表进行排序,但我觉得好像有一种更有效的解决方案.
My apologies, it seems that I did not clarify the question enough. What I want is an ordered infinite list, and while I could sort a finite list, I feel as if there may be a solution that is more efficient.
推荐答案
只有5的幂的原因是Haskell尝试评估a = 2 ^ 0和b = 3 ^ 0的所有可能c,并且仅当完成后,结果为a = 2 ^ 0和b = 3 ^ 1.因此,这种方式只能构造如下这样的有限列表: [a * b * c |a<-地图(2 ^)[0..n],b<-地图(3 ^)[0..n],c<-地图(5 ^)[0..n]] 代码>
给定n.
The reason why there are only powers of 5 is that Haskell tries to evaluate every possible c for a = 2^0 and b = 3^0 and only when it is finished it goes for a = 2^0 and b = 3^1.
So this way you can only construct a finite list like this:
[ a * b * c | a <- map (2^) [0..n], b <- map (3^) [0..n], c <- map (5^) [0..n] ]
for a given n.
这篇关于您如何找到所有数字的列表,这些数字是2、3和5的幂的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!