Almost Prime number

Problem:A no is said to be  k-Almost Prime Number if it  has exactly k prime factors (not necessary distinct). Your task is to complete the functionprintKAlmostPrimes which takes two argument k and N and prints the  first N numbers that are k prime.

Problem Link

/*You are required to complete this function*/

bool isPrime(int n)
{
    bool arr[20];
    int i;
    arr[2]=1;arr[3]=1;arr[5]=1;arr[7]=1;
    arr[11]=1;arr[13]=1;arr[17]=1;arr[19]=1;
    
    if(n<20)
    {
        if(arr[n]==1)
        return 1;
        else if(arr[n]==0)
        return 0;
    }
    if(n%2==0)
    return 0;
    for(i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
        return 0;
    }
    return 1;
}
int CountOfPrime(int n)
{
    int cnt=0,i;
    while(n%2==0)
    {
        cnt+=1;
        n/=2;
    }
    for(i=3;i<=sqrt(n);i+=2)
    {
        if(isPrime(i)==0)
        continue;
        while(n%i==0)
        {
            cnt+=1;
            n/=i;
        }
    }
    if(n>2)
    cnt+=1;
    return cnt;
}
void printKAlmostPrimes(int k, int n)
{
//Your code here
    int i,num;
    for(i=1,num=2;i<=n;num++)
    {
        if(CountOfPrime(num)==k)
        {
            printf("%d ",num);
            i++;
        }
    }
}

 

Comments are closed.