# Brilliant Numbers in Java

In this section, we will learn what is brilliant number and also create Java programs to check if the given number is a brilliant number or not. The brilliant number program is frequently asked in Java coding interviews and academics.

## Brilliant Number

A number is called a brilliant number if the product of two prime numbers (say p and q) such that a total number of digits present in the prime number p must be equal to the total number of digits present in the prime number q.

Since a brilliant number is a product of two prime numbers, therefore, each brilliant number is also a semi-prime number. However, vice-versa is not true. Mathematically, N is said to be a brilliant number iff

N = p * q and Number of digits in p = Number of digits in q.

Let's understand it through examples.

Example 1:

Input: 15

Output: 15 is a brilliant number.

Explanation: 15 is the product of two prime numbers 3 and 5. Also, the number of digits in the number 3, which is 1, is equal to the number of digits in the number 5. Hence, 15 is a brilliant number.

Example 2:

Input: 50

Output: 50 is a not brilliant number.

Explanation: 50 is the product of two numbers 5 and 10. However, 10 is not a prime number. Hence, 50 is not a brilliant number.

Example 3:

Input: 22

Output: 2 is a not brilliant number.

Explanation: 22 is the product of two prime numbers 2 and 11. Also, the number of digits in the number 2, which is 1, is not equal to the number of digits in the number 11, which is 2. Hence, 2 is not a brilliant number.

## Validation For the Brilliant Number

To check whether n is a brilliant number or not, we need to check whether the number is semi-prime or not.

• If the number is not a semi-prime number, then it can never be a brilliant number.
• If it is a semi-prime number, then check whether the total count of digits of the prime numbers is equal or not.

Observe the following algorithm.

Step 1: Take the number n.

Step 2: Compute the total number of factors for the number n.

Step 3: Check whether the total count of factors computed in step 2 is equal to 3 or 4 or not (condition for checking whether the number is semi-prime or not).

• If the total count of factors is equal to 3, then check whether the number n is a perfect square or not. If yes, then the number is a brilliant number; otherwise, not.
• If the total count of factors is equal to 4, then check the total number of digits of the proper factors. If the proper factors have the same digit count, then the number is a brilliant number; otherwise, not.
• If the total count is neither equal to 3 nor equal to 4, then the number is not a brilliant number.

Let's implement the above algorithm in a Java program.

## Brilliant Number Java Program

Observe the implementation of the above algorithm.

FileName: BrilliantNum.java

Output:

```The number 15 is a brilliant number.
The number 50 is not a brilliant number.
The number 22 is not a brilliant number.
```

## Find Brilliant Numbers Within a Range

In this approach, we will be using a sieve to find the brilliant numbers within the given range. Take two prime numbers of the same digits and find their product. The product is going to be a brilliant number.

Suppose, we want to find the brilliant numbers that are present within the range 1 to 200. Observe the following algorithm.

Step 1: Compute the sieve to find the prime numbers between 1 to 20.

Step 2: Filter out the prime numbers and store them in an array or an ArrayList, which is al in our case.

Step 3: Start a loop i = 0 to s - 1, where s is the total size of the array

Step 4: Within the loop started in step 3, start another loop j = I + 1 to size.

Step 5: Ensure that the digit count of the number pointed by i is equal to the digit count of number pointed by j.

Step 6: Find the product of the numbers pointed by the loop variables I and j. If the product is greater than the number 200, discard it; otherwise, keep it.

Let's implement the above algorithm in a Java Program.

FileName: BrilliantNum1.java

Output:

```The brilliant numbers that lie within the range 1 to 200 are:
4 6 9 10 14 15 21 25 35 49 121 143 169 187
```