Sphenic Number | Practice | GeeksforGeeks

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.
 
Example 1:
Input: N = 30
Output: 1
Explanation: 30 = 2 * 3 * 5 so N is 
product of 3 distinct prime numbers.

Example 2:
Input: N = 60
Output: 0
Explanation: 60 = 2 * 2 * 3 * 5 so N is
product of 4 prime numbers.

 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function isSphenicNo() which takes N as input parameter and returns 1 if N is Sphenic Number otherwise returns 0.
 
Expected Time Complexity: O(N* log(N))
Expected Space Complexity: O(N)
 
Constraints:
1 <= N <= 1000
0
vivekmehla32 months ago
Time Complexity: O(n)
Space Complexity: O(n)
Time Taken:0.00sec
 int isSphenicNo(int n){    // Code here    vector<int> v;    for(int i=2;i<n;i++)    {        int count=0;        while(n%i==0)        {            n=n/i;            count++;        }        if(count>0)        {            v.push_back(count);        }    }    if(n>0)    {        v.push_back(1);    }    if(v.size()>3 || v.size()<3)    {        return 0;    }    else    {        for(int i=0;i<v.size();i++)        {            if(v[i]!=1)            {                return 0;            }        }        return 1;    }}
0
ayazmrz982 months ago
 static boolean isprime(int n)
   {
       if(n<=1)
       {
           return false;
       }
       for(int i=2;i<=Math.sqrt(n);i++)
       {
           if(n%i==0)
           {
               return false;
           }
       }
       return true;
   }
   public int isSphenicNo(int N)
   {
       
       int count=0;
       int mul=1;
       for(int i=2;i<+N;i++)
       {
           if(N%i==0)
           {
               if(isprime(i))
               {
                   count++;
                   mul=mul*i;
               }
           }
       }
       
       if(count==3 && mul==N)
       {
           return 1;
       }
       else
       {
          return 0;   
       }
   }
0
showpnilkumar211020013 months ago
//Easy soln:
class Solution{public:bool check(int n){    if(n==0||n==1)    return 0;    for(int i=2;i<=sqrt(n);i++)    {        if(n%i==0)        return 0;    }    return true;}int isSphenicNo(int N){    int count=0;    int p=1;    for(int i=2;i<=N;i++)    {        if(N%i==0)        {            if(check(i)==true)            {                count++;                p=p*i;                            }        }    }    if(count==3 &&p==N)    return 1;    else    return 0;}
0
dileep kumar10 months ago
dileep kumar
pythonexecution time: 0.06def ISprime(n):    for i in range(2,n):        if n % i == 0:            return 0    return 1class Solution:def isSphenicNo(self, N):# Code herel = []for i in range(2,N):
    if N % i == 0 and ISprime(i):        l.append(i)        if len(l)<3:            return 0        if len(l)>=3:            for i in range(0,len(l)-2):                if N % l[i] == 0:                    if (l[i]*l[i+1]*l[i+2]==N):                        return 1                    else:                        continue                else:                    continue            return 0
0
Yash Patil2 years ago
Yash Patil
Best Solution Execution Time 0.29:public static void main (String[] args) { int n=10000;        boolean []prime=new boolean[(n+1)];        int count[]=new int[(1000)];        for(int i=0;i<=n;i++)        {            prime[i]=true;        }        for(int i=2;i*i<=n;i++)        {            if(prime[i]==true)            {                for(int j=i*i;j<=n;j+=i)                {                     prime[j]=false;                }            }        }        int val=0;        for(int i=2;i<=n;i++)        {            if(val>=count.length)            {                  break;            }            if(prime[i]==true)            {                count[val++]=i;            }        }          Scanner sc1 =new Scanner(System.in);        int test=sc1.nextInt();        while(test!=0)        {
            int m=sc1.nextInt();        TreeMap<integer,integer> tm1 =new TreeMap<integer,integer>();        int res=0,j=0;        int p=count[j];        while(p*p<=m)        {            res=0;              if(m%p==0)              {                  while(m%p==0)                  {                      m/=p;                      res++;                  }                  tm1.put(p,res);              }              j++;              p=count[j];        }        if(m!=1)        {           tm1.put(m,1);        }
        if(tm1.size()>3)        {             System.out.println(0);        }        else        {            boolean flag=true;        for(Map.Entry<integer,integer> x1:tm1.entrySet())        {              if(x1.getValue()>1)              {                   flag=false;                   break;              }        }        if(flag==true&&tm1.size()==3)        {           System.out.println(1);        }        else        {            System.out.println(0);        }
        }            test--;        }
}
0
Ranadheer2 years ago
Ranadheer
c++ solutionhttps://ide.geeksforgeeks.o...
0
Riya kashyap2 years ago
Riya kashyap
solution in javahttps://ide.geeksforgeeks.o...
0
This comment was deleted.
0
prerna singh2 years ago
prerna singh
https://ide.geeksforgeeks.org/YPFZ4n21Cw0.01
0
Ajay Agrawal3 years ago
Ajay Agrawal
https://ide.geeksforgeeks.o...execution time : 0.01sec
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.