Results 1 to 5 of 5
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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:

    Java 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?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Feedback on my PrimeNumber Finder code please.

    I am trying it doing without controller, but I am not very successful at it :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

    Java 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.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Feedback on my PrimeNumber Finder code please.

    Thanks..
    I will try this, and hopefully get the idea.

    Thank you.

Similar Threads

  1. PrimeNumber
    By zit1343 in forum New To Java
    Replies: 4
    Last Post: 05-01-2011, 07:52 PM
  2. Path Finder
    By OmerHalit in forum AWT / Swing
    Replies: 2
    Last Post: 04-05-2010, 08:09 PM
  3. Java primenumber application
    By Crossfires in forum New To Java
    Replies: 9
    Last Post: 10-12-2009, 06:06 PM
  4. sign finder
    By iPetey in forum New To Java
    Replies: 6
    Last Post: 05-01-2009, 05:24 AM
  5. Library Finder 1.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-15-2007, 04:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •