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.
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.
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.
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.
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.
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).
Let's implement the above algorithm in a Java program.
Brilliant Number Java Program
Observe the implementation of the above algorithm.
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.
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
Next TopicSort Elements by Frequency in Java