Results 1 to 12 of 12
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default Prime Number finder

    Hello i am trying to write a program to find all the prime numbers in a given number range but i cant seem to do it. Here is what i have so far, i think im close but just cant get it right

    Java Code:
     
    public class MainClass 
    { 
     public static ArrayList<Integer> interval( int m, int n ) 
      { 
        ArrayList<Integer> result = new ArrayList<Integer>();   
      	int a= m-1; 
       
        for(int i = m; i<=n; i++ ) 
        { 
      	a++;   
        	result.add(a); 
         
        } 
       
        return result; 
      } 
    
     public static boolean divides( Integer a, Integer b ) 
      { 
        return ((b % a) == 0); 
      } 
    
    public static ArrayList<Integer> primes( int t )
    {
      ArrayList<Integer> plist = interval( 2, t );
      int m = 0;
      while ( m < plist.size() )
      {
        int n = m;
        while ( n < plist.size() )
        {
          if ( divides( plist.get( m ), plist.get( n ) ) )
            plist.remove( n );
          else 
            n++;
        }
        m++;
      }
    
      return plist;
    }
    
    
    public static void main( String[] args )
    {
      System.out.println( primes( 20 ) );
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can do this in a much simpler manner. Try creating a static method which takes some number and determines if it is prime
    Java Code:
    public static boolean isPrime(int n)
    after you do that the problem is fairly trivial, all you will need to do from there is ask for 2 inputs and loop from the one to the other testing isPrime on each.

    Also what errors are you getting?

    What is interval supposed to do? It seems like it may be a waste.

    Is the goal to find all primes from 1 - n? or from n1 - n2?
    Last edited by sunde887; 02-13-2011 at 10:18 PM.

  3. #3
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    interval is accepting to inputed numbers and all numbers in that range are imported into the array list.

    and yes i am trying to find the primes from n1-n2

    and im not getting any errors i am just not getting the correct prime numbers outputted

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    if ( divides( plist.get( m ), plist.get( n ) ) )
    When the method reaches this statement, whats m? Whats n?
    while filling an arraylist of all the possibles is a decent idea it's a bit overcomplicated.

    Whats a prime number? How do you determine them? Also, are you getting compile time errors, or runtime exceptions? Post what they say.

    Are you taking into account that when you remove an item from an arraylist all indexes are shifted down?

    My biggest piece of advice for you is this, start small when you write your programs, make something fairly basic and move your way up until you reach your goal.

    For this problem start by figuring out how to determine if a number is divisible by another, you got that part so you can not determine how to test if a number is prime, do that and post just that code.
    Last edited by sunde887; 02-13-2011 at 10:38 PM.

  5. #5
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    m is currently the first element in the arraylist and n is also the first element in the array list

    here is how i would determine a prime number
    Java Code:
    boolean isPrime(int n) {
        //check if n is a multiple of 2
        if (n%2==0) return false;
        //if not, then just check the odds
        for(int i=3;i*i<=n;i+=2) {
            if(n%i==0)
                return false;
        }
        return true;
    if it is not prime though i also need to take it out of the arraylist. I need to print the the arraylist at the end that only contains the prime numbers in the interval

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    now create main, and pass some common prime numbers to your method to make sure it's working correctly(1, 3, 5, 7, 11), and pass some non prime numbers, the more you try the better you can test if you have successfully defined the method. From there you can now create(in main) the interval testing, or you can do it in a separate method.

    Why did you choose to use a filled array list?
    Are you taking into account that when you remove an item from an arraylist all indexes drop down one?

  7. #7
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    ok so i figured it out finally haha

    i needed to change the prime class to this
    Java Code:
    public static ArrayList<Integer> primes( int t ) 
      { 
        ArrayList<Integer> plist = interval( 2, t ); 
        int m = 0; 
        while ( m < plist.size() ) 
        { 
          int n = m; 
          while ( n + 1 < plist.size() ) 
          { 
            if ( divides( plist.get( m), plist.get( n+1 ) ) ) 
             plist.remove( n + 1 ); 
            else  
       	 n++; 
          } 
          m++; 
        } 
       
        return plist; 
      }
    i was just calculating the prime number incorrectly

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Interesting.

    I would think that only adding prime numbers to a List would be easier than removing non-primes.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What junky said was what I was hinting out, your approach is unique, and Im glad you solved it, but the simpler way would be to create an empty array list, adding items that are prime.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Mind you OP is kinda doing a sieve of Eratosthenes.

  11. #11
    k_nica is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    You'll have to change to what ever method you use to input or output a value. I'm using IO.java.
    Other than that, works like a charm.


    {{{

    public class NthPrime
    {
    public static void main(String[]args)
    {
    int count=0;
    int numTest=1;
    int finalPrime=0;

    System.out.println("Enter a value 'N' to find the 'Nth' prime number.");
    int primeN = IO.readInt();

    if(primeN == 0)
    {
    IO.reportBadInput();
    return;
    }

    do
    {
    double limit = Math.sqrt(numTest);
    boolean prime = true;

    if(numTest==1)
    {
    prime = false;
    }

    else if(limit<2)
    {
    prime=true;
    }

    else
    {
    for(int divisor = 2; divisor <= limit && prime; divisor++)
    {
    if(numTest % divisor == 0)
    {
    prime = false;
    break;
    }
    /*
    else
    {
    prime=true;
    finalPrime = numTest;
    }
    */
    }
    }

    if(prime==true)
    {
    count=count+1;
    finalPrime=numTest;
    numTest++;
    System.out.println("The current prime number stored is: " + finalPrime);
    }
    else
    {
    numTest++;
    }

    }while(count<primeN);

    IO.outputIntAnswer(finalPrime);
    }
    }

    }}}

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by k_nica View Post
    Other than that, works like a charm.
    Hey Hey Hey... There you are!

    No Code Tags, No code indentation and spoon feeding with ready baked code.

    Everyone here was kind of stuck up for someone to come and do this for OP. And there you arrive.

    God is Great...!!! Cheers :D
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. find a prime number
    By loja11 in forum Threads and Synchronization
    Replies: 4
    Last Post: 02-17-2011, 08:54 AM
  2. displaying prime number
    By eng_hyzoom in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 11:21 AM
  3. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 02:49 PM
  4. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 11:42 PM
  5. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM

Posting Permissions

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