1, according to the definition of prime numbers to seek

Prime number definitions: natural numbers (excluding 1), which can be divisible by only 1 or itself, are called prime numbers.

Its definition can be used to iterate over the number divided by each natural number smaller than it (greater than 1), which is not prime if it can be divisible by it.

The corresponding code is:

void Printprime (int n) {//To determine whether N is a prime number

Whether the booleanisprime=true;//is a prime symbol

for (inti=n-1;i>1;i-) {//n divided by each natural number smaller than n by 1

if (n%i==0) {//If there is divisible, it is not prime

Isprime=false;

}

}

if (IsPrime) {//If it is a prime number, print it out

System.out.print (n+ "");

primenumber++;//record number of prime numbers

if (primenumber%10==0)//output 10 prime numbers followed by a newline

System.out.println ();

}

}

2, using a theorem-if a number is composite, then its minimum factorization is certainly less than equal to his square root. For example: 50, the minimum factorization is the open root of 2,2<50

Another example: 15, the minimum factorization is the open root of 3,3<15

Composite is the natural number corresponding to prime numbers. A natural number greater than 1 if it is not a composite, then it is prime.

The above theorem is that if a number can be divisible by its smallest factorization, it must be composite, that is, not prime. So judging whether a number is a prime, just to determine whether it can be less than the number of all after the opening and after the integer division, so that the operation will be much less, so the efficiency is much higher.

The corresponding code is:

void Printprime (int n) {//To determine whether N is a prime number

Whether the booleanisprime=true;//is a prime symbol

ints= (int) math.sqrt (n);//N Open Radical

for (inti=s;i>1;i-) {//n divided by each of the 1 natural numbers that are smaller than the N open square root

if (n%i==0) {//If there is divisible, it is not prime

Isprime=false;

}

}

if (IsPrime) {//If it is a prime number, print it out

System.out.print (n+ "");

primenumber++;//record number of prime numbers

if (primenumber%10==0)//output 10 prime numbers followed by a newline

System.out.println ();

}

}

3, sieve method for prime numbers, the most efficient, but will be more waste of memory

Start by creating a Boolean array that stores the prime numbers you want to determine in a range of natural numbers, for example, if you want to output a prime number less than 200, you need to create a Boolean array with a size of 201 (to create 201 storage locations to make the array position the same size). Initialized to True.

Secondly, the first prime number in the second method (in this case, 2), and then will be a multiple of 2 is all false (except 2), that is, 2, 4, 6, 8 ... The position is set to false. Then a multiple of 3 is set to False (except 3), until 14 (14 is the open square of 200), so that the position is not prime number is false, the rest is prime, and pick the true print out on the line.

The corresponding code is:

Boolean[] Printprime (int range) {

Boolean[] isprime=newboolean[range+1];

ISPRIME[1]=FALSE;//1 is not prime.

Arrays.fill (isprime,2,range+1,true);//full-set to true (position greater than or equal to 2)

intn= (int) math.sqrt (range);//Open radical for range

for (inti=2;i<=n;i++)//note need to be less than or equal to n

if (Isprime[i])//view is not already set false too

for (int j=i;j*i<range;j++)//will be a multiple of position set to False

Isprime[j*i]=false;

returnisprime;//returns a Boolean array

}

Several algorithms for finding prime numbers