Tools: VSCode | Source Code: GitHub
The BrilliantNumbers
program is designed to determine whether a given number is a "Brilliant Number," a concept defined as a number that can be factored into two prime numbers with the same number of digits. The program interacts with the user to receive a list of numbers and checks each one individually, providing feedback on whether the number is considered "Brilliant." The program is built with user input handling, prime checking, and factor analysis in a structured way to achieve this goal.
The main method starts by using a Scanner
object to collect user input. It first asks the user how many numbers they want to check and stores this in the times
variable. Then, in a loop, the program repeatedly asks the user to input a number and checks whether each number is a Brilliant Number by calling the isBrilliant()
method. If the number meets the criteria, it prints "TRUE"; otherwise, it prints "FALSE." After processing all numbers, the Scanner
is closed to manage resources properly and avoid memory leaks.
The isBrilliant
method is the core of the program. It checks whether a number can be factored into two prime numbers, and if so, whether those prime factors have the same number of digits. The method iterates through potential factors starting from 2 up to the square root of the number. For each factor, it checks if both the factor and its paired factor (calculated as number / i
) are prime. The isPrime
method is used to verify primality. If the factors are prime and have the same number of digits, the number is identified as Brilliant and the method returns true
; otherwise, it returns false
.
The isPrime
method checks if a number is prime by testing if it is divisible by any number between 2 and its square root. If any divisors are found, the method returns false
; otherwise, it returns true
, indicating the number is prime. This method ensures that only prime factors are considered in the Brilliant Number check.
The digitLength
method converts a number to a string and returns its length, which is used to compare the number of digits between two prime factors. This simple method helps ensure that only pairs of prime factors with matching digit lengths are considered valid, which is a key part of the Brilliant Number definition.
import java.util.Scanner;
public class BrilliantNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("How many numbers would you like to check? ");
int times = scanner.nextInt();
for (int i = 0; i < times; i++) {
System.out.print("Enter number: ");
long number = scanner.nextLong();
System.out.println(isBrilliant(number) ? "TRUE" : "FALSE");
}
scanner.close();
}
public static boolean isBrilliant(long number) {
int factorCount = 0;
long firstFactor = -1;
for (long i = 2; i * i <= number; i++) {
if (number % i == 0 && isPrime(i)) {
long pairedFactor = number / i;
if (isPrime(pairedFactor)) {
if (factorCount == 0) {
firstFactor = i;
factorCount++;
} else if (factorCount == 1 && digitLength(firstFactor) == digitLength(pairedFactor)) {
return true;
} else {
return false;
}
}
}
}
return false;
}
public static boolean isPrime(long num) {
if (num < 2) return false;
for (long i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static int digitLength(long num) {
return String.valueOf(num).length();
}
}