Sphenic Number in Java

In this section, we will learn what is a sphenic number and also create Java program to check if the given number is sphenic or not. The sphenic number program frequently asked in Java coding tests and academics.

Sphenic Number

A positive integer n is called a sphenic number if the product of factors of the given number (n) is exactly three and all factors are prime. In other words, if n is a sphenic integer then n=p x q x r (p, q, and are three distinct prime numbers and their product are n). It is a sequence A007304 in the OEIS. Let’s understand it through an example.

A number will be a sphenic number if the product of three distinct prime numbers gives the number itself. The sphenic numbers have exactly 8 divisors.

Sphenic Number in Java

The eight divisors are as follows:

  1. 1
  2. Three distinct primes
  3. Three semi-primes (in which each of the distinct prime factors of the sphenic number is omitted)
  4. The sphenic number itself

Let’s consider the number 42 and check it is sphenic or not.

The factors of 42 are 1, 2, 3, 7, 21. Let’s find the 8 divisors.

  • 21 equals 3 times 7, 2 is omitted.
  • 14 equals 2 times 7, 3 is omitted.
  • 6 equals 2 times 3, 7 is omitted.
  • 42 itself.

Hence, 42 is a sphenic number because it has exactly three prime factors 2, 3, and 7 and the product of these factors gives the number itself.

Note: The product of the cube of a prime and another prime as well as seventh powers of primes also has 8 divisors.

Sphenic Number Example

Let’s take the number 30 and check if it is sphenic or not.

The smallest three primes factors that form the same numbers are 2, 3, and 5. On multiplying them, we get the same number 30. Hence, the given number is a sphenic number.

Let’s take another number, 110.

110=1,2,5,10,11,22,55,and 110

The smallest three primes factors that form the same numbers are 2, 5, and 11. On multiplying them, we get the same number 110. Hence, the given number is a sphenic number.

Let’s take another number, 23.

23=1,23

The given number 23 is not a sphenic number. Because there are only two prime factors.

Similarly, we can check other numbers also. Some other sphenic numbers are 78, 102, 105, 110, 285, 286, 290, 310, 318, 322, 345, etc. We can find the complete list of all the sphenic numbers up to 10000 provided by OEIS.

Sphenic Number Java Program

Above, we have discussed that the sphenic number has exactly 8 divisors. So, first, we will try to find if the number is having exactly 8 divisors or not. After that, we will check that the first, three digits (except 1) are prime or not.

SphenicNumberExample1.java

  1. import java.util.*;  
  2. public class SphenicNumberExample1  
  3. {  
  4. //create a global array of size 100000  
  5. static boolean arr[] = new boolean[10000];  
  6. //finds all the primes smaller than the limit  
  7. static void findPrime()  
  8. {  
  9. //marks all entries as true      
  10. //A value in mark[p] will finally be false if ‘p’ is Not a prime, else true.  
  11. Arrays.fill(arr, true);  
  12. //iterate over all the numbers so that their multiples can be marked as composite  
  13. for(int p = 2; p * p < 10000; p++)  
  14. {  
  15. //if p is not changed, then it is a prime  
  16. if(arr[p])  
  17. {  
  18. //update all the multiples of p  
  19. for(int i = p * 2; i < 10000; i = i + p)  
  20. arr[i] = false;  
  21. }  
  22. }  
  23. }  
  24. //user-defined function that checks if the given number is sphenic or not  
  25. static int isSphenic(int N)  
  26. {  
  27. //creating an array that stores the 8 divisors      
  28. int []arr1 = new int[8];   
  29. //counts the divisors  
  30. int count = 0;    
  31. int j = 0;  
  32. for(int i = 1; i <= N; i++)    
  33. {  
  34. if(N % i == 0 && count < 8)    
  35. {  
  36. //increments the count by 1      
  37. count++;  
  38. arr1[j++] = i;  
  39. }  
  40. }  
  41. //checks that there is exactly 8 divisors or not and all the numbers are distincit prime or not  
  42. //if yes returns 1, else returns 0  
  43. if(count == 8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))  
  44. return 1;  
  45. return 0;  
  46. }  
  47. //driver code  
  48. public static void main(String args[])  
  49. {  
  50. //calling user-defined function that find the priime numbers  
  51. findPrime();  
  52. Scanner sc=new Scanner(System.in);  
  53. System.out.print(“Enter a number to check: “);  
  54. //reading an iteger from the user  
  55. int n=sc.nextInt();  
  56. int result = isSphenic(n);  
  57. if(result == 1)  
  58. //prints yes if the above condition returns true  
  59. System.out.print(“Yes, the given number is sphenic.”);  
  60. else  
  61. //prints no if the above condition returns false  
  62. System.out.print(“No, the given number is not a sphenic.”);  
  63. }  
  64. }  

Output 1:

Enter a number to check: 165
Yes, the given number is sphenic.

Output 2:

Enter a number to check: 18967
No, the given number is not a sphenic.

Let’s find all the sphenic numbers between the given range.

SphenicNumberExample2.java

  1. import java.util.*;  
  2. public class SphenicNumberExample2  
  3. {  
  4. public static void main(String args[])   
  5. {  
  6. Scanner sc=new Scanner(System.in);  
  7. int lower, upper, i, n, f, count, k;  
  8. System.out.print(“Enter the lower limit: “);  
  9. //reads the lower limit from the user  
  10. lower=sc.nextInt();  
  11. System.out.print(“Enter the upper limit: “);  
  12. //reads the upper limit from the user  
  13. upper=sc.nextInt();  
  14. System.out.println(“\nSphenic numbers between the given range are: “);  
  15. for(i=lower;i<=upper;i++)  
  16. {  
  17. n=i;  
  18. k=0;  
  19. //defining an array that stores distinct prime factors  
  20. int prime[]={0,0,0};   
  21. //finds all the prime factors  
  22. for(f=2; n!=1;f++)      
  23. {  
  24. //counts the frequency of the prime factors      
  25. count=0;                  
  26. while(n%f==0)  
  27. {  
  28. count++;              
  29. n=n/f;  
  30. }  
  31. if(count==1)           
  32. prime[k++]=f;  
  33. if(k==prime.length)    
  34. //breaks the execution if there are 3 unique prime factors  
  35. break;            
  36. }  
  37. //multiplying the prime factors  
  38. n=prime[0]*prime[1]*prime[2];  
  39. //compares the product (n) with the original number (i)  
  40. if(i==n)            
  41. System.out.print(i+” “);  
  42. }  
  43. System.out.println();  
  44. }   
  45. }   

Output:

Sphenic Number in Java

Let’s create another Java program to find all the sphenic numbers by using different logic.

SphenicNumberExample3.java

  1. import java.util.*;  
  2. public class SphenicNumberExample3  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. Scanner sc=new Scanner(System.in);  
  7. int x,num,i,j,a,b,cp,ctr;  
  8. System.out.print(“Enter the lower limit: “);  
  9. a=sc.nextInt();  
  10. System.out.print(“Enter the upper limit: “);  
  11. b=sc.nextInt();  
  12. for(num=a;num<=b;num++)  
  13. {  
  14. int c=0, f=1;  
  15. cp=num;  
  16. ctr=0;  
  17. for (x=2;x<=cp;x++)  
  18. {  
  19. c=0;  
  20. while((cp%x)==0)  
  21. {  
  22. cp=cp/x;  
  23. c++;  
  24. }  
  25. if(c==1)  
  26. {  
  27. f=f*x;  
  28. ctr++;  
  29. }  
  30. }  
  31. if(f==num && ctr==3)  
  32. System.out.print(num+”\t”);  
  33. }  
  34. }  
  35. }  

Output:

Sphenic Number in Java

Leave a Comment