# Feedback on my PrimeNumber Finder code please.

• 12-22-2011, 10:59 PM
fatabass
Feedback on my PrimeNumber Finder code please.
Hi guys.

I am a beginner in programming, and trying to learn JAVA.

I wrote this code myself where I am looking for prime numbers and printing them on the screen:

Code:

```package myPackage; import java.util.ArrayList; public class TestClass {                 public static void main(String[] args) {                                 ArrayList<Integer> primeNumbers = new ArrayList<Integer>();                                 primeNumbers.add(2);                 primeNumbers.add(3);                                 int m;                                 ArrayList<Integer> controller = new ArrayList<Integer>();                                 for(int isThisAPrimeNumber=4;isThisAPrimeNumber<40;isThisAPrimeNumber++)                 {                         for(int k=0;k<primeNumbers.size();k++)                         {                                 m = primeNumbers.get(k);                                 controller.add(isThisAPrimeNumber%m);                         }                         if(controller.contains(0))                         {                                 controller.clear();                         }                         else                         {                                 primeNumbers.add(isThisAPrimeNumber);                                 controller.clear();                         }                 }                 for(int t:primeNumbers)                 {                         System.out.println(t);                 }         } }```
It works fine, but I have no experienced people around me, so there is no one to say, nobody would do it like this.

Can you have a peek and tell me if this is ok?
• 12-22-2011, 11:15 PM
pbrockway2
Re: Feedback on my PrimeNumber Finder code please.
It's a reasonable approach provided you realise that it relies on building up a list of primes.

Notice that you find *all* of the prime factors of each number (these are the ones where test%m is zero) but in fact you know that the number being tested is *not* a prime once you have found the first prime factor. You could break out of the inner for loop at this point. With that change it might make sense to remove controller altogether.
• 12-22-2011, 11:21 PM
fatabass
Re: Feedback on my PrimeNumber Finder code please.
I am trying it doing without controller, but I am not very successful at it :)
• 12-22-2011, 11:43 PM
pbrockway2
Re: Feedback on my PrimeNumber Finder code please.
That's less important than working on the fact that you can act once you have found the first prime factor.

Code:

```for each number     isPrime = true     for each prime         if it is a prime factor             isPrime = false             break // don't check any more primes     if the number isPrime         add it to the primes list```
Does that make sense?

isPrime is behaving a bit like your controller. But it is reset just *before* the inner for loop. And it is reset to true because the function of the inner loop is to detect things which are *not* prime.
• 12-22-2011, 11:43 PM
fatabass
Re: Feedback on my PrimeNumber Finder code please.
Thanks..
I will try this, and hopefully get the idea.

Thank you.