Home » Sphenic Number in Java

Sphenic Number in Java

by Online Tutorials Library

Sphenic Number in Java

In this section, we will learn what is a sphenic number and also create Java programs 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

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

Output:

Sphenic Number in Java

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

SphenicNumberExample3.java

Output:

Sphenic Number in Java


You may also like