30
is a sphenic number because it is the product of 2, 3,
and 5
, which are all prime numbers.import java.util.Scanner;
public class SphenicNumber {
// Function to check if a number is sphenic or not
public static boolean isSphenic(int n) {
int count = 0;
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
count++;
if (count > 3) {
return false;
}
n /= i;
i--;
}
}
return count == 3;
}
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (isSphenic(num)) {
System.out.println(num + " is a sphenic number.");
} else {
System.out.println(num + " is not a sphenic number.");
}
}
}
Enter a number: 30
30 is a sphenic number.
Enter a number: 37
37 is not a sphenic number.
isSphenic()
function takes an integer n
as input and checks if it is a sphenic number or not. It uses a count
variable to keep track of the number of distinct prime factors of n
. If n
is divisible by a prime number, the count is incremented, and the factor is removed from n
. The loop continues until n
has been completely factored or the count exceeds 3. If n
has exactly 3 prime factors, the function returns true; otherwise, it returns false.isSphenic()
function to check if it is a sphenic number. If it is, we print a message indicating that it is a sphenic number; otherwise, we print a message indicating that it is not a sphenic number.