Here is the code that is using command line parameter passing:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author pacior
 */
public class MyPrimeNumber {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    boolean[] pm = new boolean[Integer.parseInt(args[0])];
    pm[0] = true;//You rather don't need to do this, but thats the way
in article
        for (int i = 1; i < pm.length; i++) {
            if ( primeNumberTest(i+1) ){
                crossOut(pm,i+1);
            }
        }
        for (int i = 0; i < pm.length-1; i++) {
            if ( pm[i] == false )
            System.out.println("Prime number:  " + (int)(i+1));
        }
    }
/**
 * The solution depends on a statement, that if you have factors of
number, then
 * the given factor f, is a sum of some integer numbers x + times. So
You can repeat adding a as long as it reach tested number
 * it is done in while loop using for, for each number ( created from
count )
 * @param primeNumber
 * @return
 * true if number is prime, false otherwise
 */
    public static boolean primeNumberTest(int primeNumber){
        if ( primeNumber == 1 )//1 is not a prime number
            return false;
        if ( primeNumber == 2 )//2 is the smallest prime number
            return true;
        int i = 2;//counter
        int j = 1;

        while ( i < Math.ceil(primeNumber/2)+1 ){//we test sum of
variable i up to primeNumber/2
            j = i;
            do {
                 j += i;
                }while ( j < primeNumber );
            if ( j == primeNumber ){
                return false;//adding sums resulted in fact, that
primeNumber is that sum - that is not a prime number!
            }
                i++;
        }
        return true;
    }
    public static void crossOut(boolean[] a,int num){
        int num2 = num+num;
        while ( num2 < a.length ){
            a[num2-1]= true;//a[3]=4
            num2 += num;
        }
    }
}

It use static methods and it is based on this article:
http://mathforum.org/dr.math/faq/faq.prime.num.html

Pacior

On Jul 22, 3:35 pm, nguyen thanh lam <[email protected]> wrote:
> Help me plz ! How to find Prime number ?

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to