×

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

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.