×

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

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 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.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

India Map Pattern in Java

India Map Pattern is the pattern we'll code now using Java, as demonstrated. We will use a star in Java to print our India map.The specialty is that we will only...

4 minutes read.