Get numbers that have only 2,3 and 5 as prime factors 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++
 A: Hint $\ $ There is a very simple recursive algorithm, e.g. the final $\color{#C00}\to$ below is calculated as follows.
$\ \ 1_{2,3,5}\to 1_{3,5} 2_2\to 1_5 2_{2,3} 3\to 1_5 2_{3} 3_2 4\to 1\, 2_{3,5} 3_2 4\, 5\to 1 2_{5} 3_3 4_2 5\,6\color{#C00}\to 1 2_{5} 3_3 4\, 5_2 6\,8\to\, \cdots$  
The term $\:1 2_{5} 3_3 4_2 5\,6\:$ on the left of the $\color{#C00}\to$ means that we have computed the initial sequence $\:1\,2\,3\,4\,5\,6,\:$ where the subscript $5$ on $2$ means that $2\cdot 5$ is the next multiple of $5$ to insert, similarly $3\cdot 3$ and $4\cdot 2$ are the next multiples of $3$ and $2$ to be inserted. Therefore the next term must be $\rm\:min(2\cdot 5, 3\cdot 3, 4\cdot 2) = 8.\:$ Since  $8$ is not already at the end, we append it, then we bump the subscript $2$ from $4$ to the next term $5$, indicating that $2\cdot 5$ is the next multiple of $5$. Then recurse.
This can be implemented elegantly in a functional programming language using (lazy) streams for the multiples of $2,3,5$.
A: I'd build it up from the prime factorization. You're looking for numbers with a prime factorization of the form $2^a3^b5^c$. Thus, you can simply start with an a, b, and c of zero, and work along the following pseudocode algorithm:
do
{
   if(2^(a+1) > 3^(b+1))
      if(3^(b+1) > 5^(c+1))
      {
         increment c
         b = 0
         a = 0
      }
      else
      {
         increment b
         a = 0
      }
   else
      increment a

   results.add(2^a*3^b*5^c)
}
while(results.count < N) //should loop exactly N times

A: I solve this kind of situation with this:

function getIdealNums($l, $r) {
    $numbers = [];
for ($x=0; $x <= 2*pow(10, 5); $x++) { 
        for ($y=0; $y <= 2*pow(10, 5); $y++) { 
        $number = pow(3, $x)*pow(5, $y);
        if ($l <= $number) {
            if ($number <= $r) {
                array_push($numbers, $number);
            } else {
                break;
            }
        }
    }
}
return count($numbers);
}

A: I get similar type of question on competitive programming, although I'm beginner, my code is not much optimized but I think it will help.
// 1 2 3 4 5 6 8 9 10 12 14 15 16 18 20 21......etc// 
// 1 2 3 4 5 6 7 8 9  10 11 12 13 14 15 16......etc//

#include<iostream>
using namespace std;

int main()
{
  int n;
  int size = 1000;
  int arr[size];
  int count = 1;
  int str = 1;
  int temp = 0;

  cin>>n;
  temp = n;

  while(count <= temp)
  {
    //cout<<"looping count : "<<count<<endl;

    if(temp == 1)
    {
      arr[count] = str;
    }
    if(count == 1)
    {
      arr[count] = str;
    }
    else if(count > 1)
    {
      str += 1;

      if(str % 2 == 0 || str % 3 == 0 || str % 5 == 0)
      {
        arr[count] = str;
      }
      else
      {
        str += 1;
        arr[count] = str;
      }

    }

    count += 1;
  }

  for(count = 1; count <= temp; count++)
    cout<<"Array at "<<count<<" "<<arr[count]<<endl;

  cout<<"End str : "<<str<<endl;

  return 0;
}

A: Make a loop over numbers i.  Check the numbers one at a time, it doesn't need to be recursive.
Added:
If you want, you can use a nice recursive algorithm f(i) which is true or false depending on whether the number i should be allowed into the list.
f(i):
if i == 1: return true
else if i % 2 == 0: return f(i/2)
else if i % 3 == 0: return f(i/3)
else if i % 5 == 0: return f(i/5)
else: return false
Added:
Start the loop at 2, otherwise this function will wrongly allow 1 into the list.
