Search⌘ K
AI Features

Solution Review: Check for Prime Number

Understand how to implement a recursive method in Java to determine if a number is prime. Explore the base cases for prime checking and how recursion iterates through possible divisors. This lesson helps you apply recursion concepts specifically for prime number problems in coding interviews.

Solution: Is it a Prime Number?

Java
class ChallengeClass {
public static boolean isPrime(int num, int i) {
// First base case
if (num < 2) {
return false;
}
// Second base case
else if (i == 1) {
return true;
}
// Third base case
else if (num%i == 0) {
return false;
}
// Recursive case
else {
return isPrime(num, i-1);
}
}
public static void main( String args[] ) {
int input = 13;
boolean result = isPrime(input, input/2);
// Print if the number is prime
if (result == true) {
System.out.println(input + " is a prime number");
}
// Prints if the number is NOT a prime number
else {
System.out.println(input + " is NOT a prime number");
}
}
}

Understanding the Code

In the code above, the method ...