Sphenic Number in Java

(Last Updated On: 13/10/2023)

Sphenic number in Java are unique and fascinating mathematical entities that hold special properties and applications. In this section, we will delve into the definition and characteristics of Sphenic numbers, exploring their significance in various fields.

What is Sphenic number in Java?

Sphenic numbers are a unique class of composite numbers that result from multiplying three distinct prime numbers. These numbers, represented by the symbols p * q * r, possess eight factors, are singular in nature, and cannot be divided by any prime number more than once. In this blog post explains the idea and offers Java programming guidelines for identifying them.

Sphenic Numbers Examples 

Examples of Sphenic number in Java include:

  1. 30: It is the product of 2 * 3 * 5, where 2, 3, and 5 are prime numbers.
  2. 42: It is the product of 2 * 3 * 7, where 2, 3, and 7 are prime numbers.
  3. 66: It is the product of 2 * 3 * 11, where 2, 3, and 11 are prime numbers.

Also, learn about Strong Number in Java here!

Java Program to check if a number is a Sphenic number using Java

import java.util.*;  
public class SphenicNumberExample1  
{  
//create a global array of size 100000  
static boolean arr[] = new boolean[10000];  
//finds all the primes smaller than the limit  
static void findPrime()  
{  
//marks all entries as true      
//A value in mark[p] will finally be false if 'p' is Not a prime, else true.  
Arrays.fill(arr, true);  
//iterate over all the numbers so that their multiples can be marked as composite  
for(int p = 2; p * p < 10000; p++)  
{  
//if p is not changed, then it is a prime  
if(arr[p])  
{  
//update all the multiples of p  
for(int i = p * 2; i < 10000; i = i + p)  
arr[i] = false;  
}  
}  
}  
//user-defined function that checks if the given number is sphenic or not  
static int isSphenic(int N)  
{  
//creating an array that stores the 8 divisors      
int []arr1 = new int[8];   
//counts the divisors  
int count = 0;    
int j = 0;  
for(int i = 1; i <= N; i++)    
{  
if(N % i == 0 && count < 8)    
{  
//increments the count by 1      
count++;  
arr1[j++] = i;  
}  
}  
//checks that there is exactly 8 divisors or not and all the numbers are distincit prime or not  
//if yes returns 1, else returns 0  
if(count == 8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))  
return 1;  
return 0;  
}  
//driver code  
public static void main(String args[])  
{  
//calling user-defined function that find the priime numbers  
findPrime();  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter a number to check: ");  
//reading an iteger from the user  
int n=sc.nextInt();  
int result = isSphenic(n);  
if(result == 1)  
//prints yes if the above condition returns true  
System.out.print("Yes, the given number is sphenic.");  
else  
//prints no if the above condition returns false  
System.out.print("No, the given number is not a sphenic.");  
}  
}  

Explanation of the code:

The first thing the program does is create a boolean array of size 10,000 entries and mark each one as true. To identify prime numbers, it then iterates over the numbers and updates their multiples as false. 

The user-defined function, Sphenic, then determines whether a number has exactly 8 divisors and whether each of those divisors is a unique prime number. It returns 1 if the condition is satisfied and 0 otherwise.

To find prime numbers, the program uses the find Prime function in the main function. It asks the user to input a number, which it then sends to the isSphenic function. Depending on the outcome, it outputs whether or not the supplied number is a Sphenic number.

Output:

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

Check out our blog on Prime Number in Java here!

Java Program to find and display all the Sphenic numbers between the given range

import java.util.*;  
public class SphenicNumEx  
{  
public static void main(String args[])   
{  
Scanner sc=new Scanner(System.in);  
int lower, upper, i, num, f, count, k;  
System.out.print("Enter the lower limit: ");  
//reads the lower limit from the user  
lower=sc.nextInt();  
System.out.print("Enter the upper limit: ");  
//reads the upper limit from the user  
upper=sc.nextInt();  
System.out.println("\nSphenic numbers between the given range are: ");  
for(i=lower;i<=upper;i++)  
{  
num=i;  
k=0;  
//defining an array that stores distinct prime factors  
int prime[]={0,0,0};   
//finds all the prime factors  
for(f=2; num!=1;f++)      
{  
//counts the frequency of the prime factors      
count=0;                  
while(num%f==0)  
{  
count++;              
num=num/f;  
}  
if(count==1)           
prime[k++]=f;  
if(k==prime.length)    
//breaks the execution if there are 3 unique prime factors  
break;            
}  
//multiplying the prime factors  
num=prime[0]*prime[1]*prime[2];  
//compares the product (n) with the original number (i)  
if(i==num)            
System.out.print(i+" ");  
}  
System.out.println();  
}   
}   

Explanation of the code:

The program prompts the user to enter the range’s lower and upper limits. It then goes through each number in the range and checks to see if it is a Sphenic number. 

The code finds each number’s distinct prime factors by dividing it repeatedly by prime numbers beginning with 2. The frequency of each prime factor is counted, and if it appears exactly once, it is considered a distinct prime factor. If the code discovers three distinct prime factors, it multiplies them together to obtain the product (num). If the product (num) equals the original number (I) it is a sphenic number and is displayed. 

In order to better understand these unique numbers and their properties, this program makes it simple to locate and print all the Sphenic numbers within a given range.

Output:

Enter the lower limit: 10
Enter the upper limit: 100
Sphenic numbers between the given range are: 
30 42 66 70 78 

Get complete Java Programming Exercises and Solutions here!

Importance and applications of Sphenic number in Java

Sphenic numbers are useful in mathematics, cryptography, and prime factorization.

Sphenic numbers aid in understanding prime numbers’ distribution, relationships, patterns, and structures, aiding researchers in solving number theory mysteries.

In cryptography, Various encryption algorithms and cryptographic systems make use of sphenic numbers. Their unique characteristics and mathematical properties make them ideal candidates for generating secure keys and protecting sensitive information.

Furthermore, Sphenic number in Java are often employed in prime factorization algorithms. The prime factorization of Sphenic numbers can be utilised to solve complex mathematical problems, simplify equations, and uncover hidden patterns within numerical sequences.

Overall, sphenic numbers hold great importance and find applications in diverse areas of mathematics, cryptography, and computer science. 

We hope you found our  blog on “Sphenic Number in Java” insightful about Java Programming.  For more programming-related blogs, visit Newtum’s website.

About The Author

Leave a Reply