Sphenic Number

PROBLEM :
A Sphenic Number is a positive integer N which is product of exactly three distinct primes. The first few sphenic numbers are 30, 42, 66, 70, 78, 102, 105, 110, 114, …
Given a number N, your task is to find whether it is a Sphenic Number or not.

Examples:

Input : 30
Output : 1
Explanation : 30 is the smallest Sphenic number,
           30 = 2 × 3 × 5
           the product of the smallest three primes

Input : 60
Output : 0
Explanation : 60 = 22 x 3 x 5
              has exactly 3 prime factors but
              is not a sphenic number

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each line contains an integer N.

Output:
For each test case in a new line print 1 if N is a Sphenic Number else print 0.

Constraints:
1<=T<=100
1<=N<=1000

Example:
Input:
2
30
60

Output:
1
0

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Prime_Factors_Powers(int ) ;
int main()
{
    int t,no ;
    cin>>t ;
    while(t--)
    {
        cin>>no ;
        cout<<Prime_Factors_Powers(no) ;
        cout<<endl ;
    }
return 0;
}

int Prime_Factors_Powers(int no)
{
    int i,j,arr[no+1],ans ;
    arr[0]=0 ;
    arr[1]=1 ;
    ans=0 ;
   
    for(i=0;i<=no;i++)
        arr[i]=1 ;
       
    for(i=2;i<=no;i++)
    {
        for(j=i+1;j<=no;j++)
            if(j%i==0)
                arr[j]=0 ;
    }  
   
    for(i=2;i<=no;i++)
    {
        if(arr[i]==1)
        {
            if(no%arr[i]==0)
            {
                no=no/i ;
                ans++ ;
            }
        }
        if(no==1)
            break ;
    }
   
    if(ans==3)
        return 1 ;
    return 0 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )