4
$\begingroup$

I am given an integer N. I have to find first N elements that are divisible by 2,3 or 5, but not by any other prime number.

N = 3 Results: 2,3,5 N = 5 Results: 2,3,5,6,8 

Mistake number = 55.. 55/5 = 11.. 11 is prime number.. so means that it divides by any other prime number and doesn't counts in..

I guess there is need of recursive function,but I cant imagine what would algorithm look like

I was sent here from stackoverflow.

They gave me advice.. 2^i*3^j*5^k , for i,j,k = 0,1,2... , but it won't give an ordered array, because 2*2 < 2*3 .. and so on

I am trying to get this work in C++

  • 0
    http://en.wikipedia.org/wiki/Regular_number could be useful.2012-09-16
  • 4
    [Section 6.4 of *Higher-Order Perl*](http://hop.perl.plover.com/book/pdf/06InfiniteStreams.pdf) has an implementation of an efficient algorithm for this problem, in Perl.2012-09-17

4 Answers 4