您如何找到所有数字的列表,这些数字是2、3和5的幂的倍数?

编程入门 行业动态 更新时间:2024-10-27 12:30:47
本文介绍了您如何找到所有数字的列表,这些数字是2、3和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的幂的倍数?

本文发布于:2023-11-29 19:24:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   倍数   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!