I need to generate 1,2,3,4,5,6,8,9,10,12,15,16 etc (a given n numbers) (containing only powers of 2,3 and 5) in ascending order in linear time(O(n)).
I have the following code(I'm not very sure of its complexity,would also appreciate an answer on its complexity)
bool s_a (int a)
{
    while (a % 2 == 0)
        a = a / 2;
    while (a % 3 == 0)
        a = a / 3;
    while (a % 5 == 0)
        a = a / 5;
    if (a == 1)
        return true;
    else
        return false;
}
void s_b (int n)
{
    unsigned int k = 1,nc = 1;
    while ( k <= n)
    {
        if(s_a(nc) == true)
        {
            cout << nc << " ";
            k++;
        }
        nc++;
    }
}
      
 
Aucun commentaire:
Enregistrer un commentaire