Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the list of all numbers that are multiples of only powers of 2, 3, and 5? [duplicate]

I am trying to generate a list of all multiples which can be represented by the form 2^a*3^b*5^c, 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..] ] 

but it only lists powers of 5 and never goes on to 2 or 3.

Edit: 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.

like image 567
robbie Avatar asked Dec 11 '22 04:12

robbie


1 Answers

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.

like image 174
KacperFKorban Avatar answered Dec 12 '22 18:12

KacperFKorban