I’m receiving a logic error when trying to find all the prime numbers in this array on Codecademy:
import java.util.Arrays;
import java.util.ArrayList;
class PrimeDirective {
// Add your methods here:
public void findPrime(int[] nums) {
ArrayList<Integer> primeNumbers = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
if ((nums[i] % 2 != 0 || nums[i] % 3 != 0 ) || nums[i] % 5 != 0) {
primeNumbers.add(nums[i]);
} else { continue; }}
System.out.println(primeNumbers); }
public static void main(String[] args) {
PrimeDirective pd = new PrimeDirective();
int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
pd.findPrime(numbers);
}
}
I expected that if the number is not divisible by either 2, 3, or 5 then it is a prime number. Instead the method is adding all of the array items to the new ArrayList that I initialised in the method.
I expect it is an issue with my maths, but it all makes perfect sense in my head.
>Solution :
the logic error that you are getting in your code is because your condition for checking prime numbers is incorrect. You are only checking if the number is not divisible by 2, 3, or 5, but that is not enough to determine if a number is prime or not. For example, the number 49 is not divisible by 2, 3, or 5, but it is not a prime number either, because it is divisible by 7. A prime number is a number that is only divisible by itself and 1. So, you need to check if the number is divisible by any other number from 2 to its square root. If it is, then it is not a prime number. If it is not, then it is a prime number.
One way to fix your code is to use a boolean variable to keep track of whether the number is prime or not, and use a for loop to check all the possible divisors from 2 to the square root of the number. If any of them divides the number without remainder, then set the boolean variable to false and break out of the loop. If none of them does, then set the boolean variable to true and add the number to the primeNumbers list. Here is an example of how you can do that:
import java.util.Arrays;
import java.util.ArrayList;
class PrimeDirective {
// Add your methods here:
public void findPrime(int[] nums) {
ArrayList<Integer> primeNumbers = new ArrayList<Integer>();
for (int num : nums) {
boolean isPrime = true; // assume the number is prime
if (num == 2) { // special case for 2
isPrime = true;
}
else if (num > 2) { // check numbers greater than 2
for (int i = 2; i <= Math.sqrt(num); i++) { // loop from 2 to square root of num
if (num % i == 0) { // if num is divisible by i
isPrime = false; // set isPrime to false
break; // break out of the loop
}
}
}
else { // handle numbers less than or equal to 1
isPrime = false; // set isPrime to false
}
if (isPrime) { // if isPrime is true
primeNumbers.add(num); // add num to the list
}
}
System.out.println(primeNumbers); // print the list
}
public static void main(String[] args) {
PrimeDirective pd = new PrimeDirective();
int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
pd.findPrime(numbers);
}
}