×

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not. The sphenic number programme is a common question in academic settings and Java coding exams.

Sphenic Number

If the sum of the factors of a specific number (n) is precisely three and all of the factors are prime, the number is said to be sphenic. Alternatively, n=p x q x r is true if n is a sphenic integer (p, q, and are three distinct prime numbers and their product are n). The OEIS lists the sequence as A007304. Let's use an illustration to help us comprehend.

If the sum of three different prime numbers produces the number, then the number is a sphenic number. There are precisely 8 divisors for the sphenic numbers.

Java implementation of the sphenic number

The 8 divisors are almost as follows:

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

Take the number 42 to determine whether it is sphenic or otherwise.

1, 2, 3, 7, and 21 make up the number 42's components. Let's discover the eight divisors.

  • 21 is equal to 3 times 7 with 2 left out.
  • 3 is left out when multiplying 2 by 7 to get 14.
  • 7 is left out when multiplying 6 by 2 and 3.
  • 42 by itself.

As a result, 42 seems to be a sphenic number since it contains exactly three prime factors—2, 3, and 7—and that number itself is the product of these factors.

Notably, there are 8 divisors inside the cube of the a prime and then another prime, as well as in the seventh powers of primes.

Sphenic Number Illustration

Let's examine whether the number 30 was sphenic or not.

2, 3, and 5 are the fewest three prime factors that add up to the same number. When we multiply them, we arrive at the same result: 30. The supplied number is therefore a sphenic number.

Take another one, 110.

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

2, 5, and 11 are the fewest three prime factors that produce the same numbers. We obtain the very same number 110 when we multiply them. The supplied number is therefore a sphenic number.

Take another, 23, for example.

23=1,23

23 is provided, despite the fact that it isn't sphenic number. mainly due to the two prime elements' simplicity.

We may also verify other numbers in a similar manner. 78, 102, 105, 110, 285, 286, 290, 310, 318, 322, 345, etc. are some other sphenic numbers. The comprehensive list of all sphenic stats up to 10,000 is available from OEIS.

Java program for Sphenic Number

The sphenic numbers has roughly eight divisors, as we discussed before. Therefore, our initial goal is to determine whether the number has exactly 8 divisors or otherwise.

SphenicNumberDemo1.java

import java.util.*;  
public class SphenicNumberDemo1  
{  
// generate a 100000 element global array
static boolean arr[] = new boolean[10000];  
// identifies all primes with smaller sizes than the limit
static void findThePrime()  
{  
// reflects the truth of all entries 
// In the end, if 'pr' is not a prime, a value in mark[p] will be false, else true.
Arrays.fill(arr, true);  
// Repeat this process for all the numbers to designate the composite nature of their multiples.
for(int pr = 2; pr * pr < 10000; pr++)  
{  
// If pr remains unchanged, it is a prime.
if(arr[pr])  
{  
// all the pr multiples should be updated.
for(int s = pr * 2; s < 10000; s = s + pr)  
arr[s] = false;  
}  
}  
}  
// user-defined function that determines whether or not a given integer is sphenic
static int isSphenic(int M)  
{  
// making an array to hold the 8 multipliers
int []arr3 = new int[8];   
// divisors are counted
int coun = 0;    
int h = 0;  
for(int s = 1; s <= M; s++)    
{  
if(M % s == 0 && coun < 8)    
{  
// increases the number by 1
coun++;  
arr3[h++] = s;  
}  
}  
// Verifies whether there are precisely 8 divisors and 
// whether each integer is a distinct prime number. 
// If yes, returns 1, otherwise returns 0.
if(coun == 8 && (arr[arr3[1]] && arr[arr3[2]] && arr[arr3[3]]))  
return 1;  
return 0;  
}  
//loading the driver code  
public static void main(String args[])  
{  
// executing user-defined code to find prime numbers
findThePrime();  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter the number to check: ");  
// taking a user-provided integer
int m=sc.nextInt();  
int res = isSphenic(m);  
if(res == 1)  
// if the previously mentioned condition is true, prints yes.
System.out.print("Yes, the number given is sphenic.");  
else  
// if the above condition returns false, output is printed as no.
System.out.print("No, the number given is not a sphenic.");  
}  
}  

Output:

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

Related Topics

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Bellman Ford Algorithm in Java

Numerous algorithms have been used in dynamic programming to determine the shortest path inside a graph. Among them are Floyd, all-pair shortest path problem, Breadth First Search, Depth First Search,...

6 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Java Thread Priority in Multithreading

As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string....

6 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.