1

I have code here that takes each int in vecArray and tries to determine if it is a prime number through a series of for loops. However when I run the program, I'm not able to successfully find/add each prime of the given array to the provided vector. What am I missing?

Vector<Integer> primeVec = new Vector<Integer>();
Vector<Integer> storage = new Vector<Integer>();
for ( int num : vecArray)  {
    //create array size of num
    resultArray = new int[num];
    for(int j = 1; j <= sqrt(num); j++)  {
        int result = num % j;
        if (result == 0)  {
             storage.add(j);
        }
    }
    //if resultArray has only two integers, then it must be prime
    int size = storage.size();
    if (size == 2)  {
       //add each 
       primeVec.add(num);
        System.out.println("You added " + num + " to the Vector for primes!");

    } 
    //print out all primes in vec
    for (int e = 0; e < primeVec.size(); e++)  {
     System.out.println("The prime in element number " + e + " is: " + primeVec.get(e));
    }
  }
 }
}
Jay
  • 185
  • 2
  • 7
  • 21
  • All numbers can be divided by 1. you should start your for loop at 2. – assylias Jan 10 '13 at 19:26
  • Even if your code works, I think you should take a look at this related question http://stackoverflow.com/questions/122105/java-what-is-the-best-way-to-filter-a-collection to make your code more clear. – Guido Jan 10 '13 at 20:05

1 Answers1

1

Your code will try dividing by 1 two times, so the resultArray.length will be at least three.

You should start your loop at j = 1 instead of j = 0, and replace resultArray with a vector<int>.

Note that factorization is not the fastest way to determine if the number is prime. You do not need to store the prime factors in order to count them. In addition, once you decided that a number is composite, you can end the loop. Finally, you do not need to try all divisors up to num: if you try divisors up to sqrt(num) and none of them divides num, then you know with certainty that the num is prime.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • the resultArray in the j for loop? – Jay Jan 10 '13 at 19:30
  • 1
    @Jay Yes, if you would like to continue along the factorization route, you should replace the `resultArray` in the `j` loop with a vector that can grow. – Sergey Kalinichenko Jan 10 '13 at 19:31
  • hmmm i'm still not getting the results that i need – Jay Jan 10 '13 at 19:48
  • @Jay You may want to post an update to your question then to show what you have after the fix. – Sergey Kalinichenko Jan 10 '13 at 19:52
  • here's what i have changed it to – Jay Jan 10 '13 at 20:02
  • 1
    @Jay You have several problems in your new code: since you stop at `sqrt` (which should be `Math.sqrt`) prime numbers will produce the count of `1`, not `2`, because you do not go all the way to the `num` which is the second divisor. Also you should either clear the `storage` before the `for j` loop, or move its declaration inside the array. Here is [a link to ideone with your fixed program](http://ideone.com/VM8T7z). – Sergey Kalinichenko Jan 10 '13 at 20:14