Joe
Joe

Reputation: 95

How to find prime numbers by using an ArrayList in java

I was working with coding in java and I am stuck in this code which I implemented for finding prime numbers .

public class PrimeList {

    ArrayList<Integer> list;

    public PrimeList(int n){

        list = new ArrayList<Integer>();

        preparePrimeList(n);
    }

    private void preparePrimeList(int n){
        int c =0;
        for (int i=0; i<=n; i++) {

            if (i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0) {
                list.add(i);
                c++;
            }
        }

        list.remove(0);
        list.remove(1);
    }
    public void printPrimeList(){
        System.out.println(list);
    }
    public boolean isPrime(int nbr){

            if (list.contains(nbr)) {
                return true;
        }
        return false;
    }


    public static void main(String[] args) {

        PrimeList primes = new PrimeList(100);
        primes.printPrimeList();
        primes.isPrime(33);

    }

}

When I run the code I get the following :

[11, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Why I get wrong output ? the out put should be like this :

2, 3, 4, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97
false

What I am doing wrong ?

Thank you

Upvotes: 0

Views: 10252

Answers (7)

ramgopalcheedu
ramgopalcheedu

Reputation: 41

import java.util.Arrays;
import java.util.List;

public class PrimeTest {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9);

        for(Integer num: numbers){

            if(isprime(num,numbers.size())){
                System.out.println(num);
            }
        }
    }

    private static boolean isprime(int n,int length) {

        for(int i=2; i<=length && i<=n ;i++){

            if(i<n && String.valueOf((float)n/(float)i).endsWith("0")){
                return false;
            }else if (n==i && n%i==0){
                return true;
            }
        }
        return false;
    }
}

Upvotes: 0

Ivan Valentini
Ivan Valentini

Reputation: 23

Try with this

ArrayList <Integer> Primes= new ArrayList<>(0); 
Integer count=0;
Integer value=0;
Integer numberOfPrimes=100;
do {
    if(isPrime(value,Primes)) {
    Primes.add(value);
    count++;
}
value++;
}while(count<numberOfPrimes);
System.out.println(Primes.toString());

static boolean isPrime(Integer value, ArrayList <Integer> Primes) {     
if(value==0) {
    return false;
}       
for(int i=0;i<Primes.size() && Primes.get(i)< Math.sqrt(value);i++) {                   
    if(value%Primes.get(i)==0 && Primes.get(i)!=1) {
        return false;
    }           
}       
return true;            
}

Upvotes: 0

Lee
Lee

Reputation: 738

Java8 version:

System.out.println(Integer.toString(2));
IntStream.iterate(3, i -> i + 2)
         .filter(potential -> IntStream.rangeClosed(3, (int)(Math.sqrt(potential)))
                                       .allMatch(n -> potential % n != 0))
         .forEach(prime -> System.out.println(Integer.toString(prime)));

Upvotes: 3

RobotKarel314
RobotKarel314

Reputation: 417

Your code is doing exactly what you tell it to do. The reason it's starting at 11 is because your prime-check is flawed. For example, 2%2 == 0, so the program thinks 2 is not prime. Similarly for 3, 5, and 7. So the program starts at 11.

You could avoid this by writing a more accurate prime-check method, not one that just checks divisibility by 2, 3, 5, or 7. For example:

public boolean checkIfPrime(int num){
   for (int x = 2; x < (int)Math.sqrt(num); x++)
      if (num % x == 0)
          return false;
   return true;
}

In addition, in trying to remove the first items from the list, you actually removed the first and third items (since the third, position 2, moved to the second, position 1). Thats why 13 was removed in your program.

Replace list.remove(1) with list.remove(0) (so you have two).

Hope this helps!

Upvotes: 2

T. Claverie
T. Claverie

Reputation: 12256

The problem is that i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0 eliminates the missing numbers, that is 2,3,5,7 because the do not meet it.

Also, you are removing the first two numbers of the list to statisfy this condition, that is 1 and 13. (11 becomes index 0 after removing 1).

However, if your code works only up to 120. After that non-prime numbers start to appear in the sequence. To compute all prime numbers up to n, you should instead look at Eratosthene's sieve.

Upvotes: 4

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

issue in remove from list:

    list.remove(0);
    list.remove(1);

So what you are doing here is removing the first element which is 1 and remove the third one which became the second one after deleting the first one which is 13

Upvotes: 3

Titus
Titus

Reputation: 22474

That is because 2, 3, 4, 5, 7 don't meet your condition 2 % 2 == 0, 3 % 3 == 0 and so on. You can change the condition to something like this:

if ((i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0) || i == 2 || i == 3 || i == 5 || i == 7){
    ....
} 

Upvotes: 3

Related Questions