5 Replies Latest reply: Sep 30, 2006 2:29 PM by 807598 RSS

    Prime numbers

    807598
      what is the source code for determining prime numbers from array list
        • 1. Re: Prime numbers
          807598
          I really doubt that this would be part of the standard API.
          You'll have to write it yourself.

          Good luck on your homework!
          • 2. Re: Prime numbers
            807598
            import java.util.*;
            
            public class CalcPrimes{
            
            ArrayList<Integer> al = new ArrayList<Integer>();
            
            public static void main(String [] args){
            CalcPrimes calc = new CalcPrimes();
            //add numbers to arraylist
            for(int l =0; l<100;l++){
            calc.al.add(l);
            }
            //sends array list to printPrime method and prints out all primes in that arraylist
            calc.printPrime(calc.al);
            }
            
            public void printPrime (ArrayList<Integer> al) {
                      for(int p = 0,d=0; p < al.size(); p++){
                           for(d=2;d<p;d++){
                                if(p%d==0)
                                break;
                           }
                                if(p==d){
                                System.out.println(p);
                                }
                           }
                 }
            
            }
            • 3. Re: Prime numbers
              807598
              I really doubt that this would be part of the
              standard API.
              You'll have to write it yourself.

              Good luck on your homework!
              BigInteger Class has a probablePrime() method.
              • 4. Re: Prime numbers
                807598
                But how to determine prime numbers after a user have input array list for example user input{10,3,11,6}......How should I find the prime numbers?
                • 5. Re: Prime numbers
                  807598
                  Are you looking for prime numbers or relatively prime numbers?