To find prime numbers by filtering method

Source: Internet
Author: User

To find prime numbers by filtering method

Here's a common method of prime numbers.

However, it is unreasonable to ask for many primes, and each number has a traversal

I found this screening method very good today.

Find all primes within a limit

Wikipedia links

V1.0

Steps:

1: Starting from 2

2:2 is the prime number, which removes a multiple of 2

3: The next number is 3, then 3 is the prime, and the number of multiples of 3 is removed.

4: The next number is 5, then 5 is the prime number, and the removal is a multiple of 5

"Why is not a multiple of 2, has been removed"

5: Just keep walking

Java Program:

    int[] SetPrime1 (intlimit) {        //Prime is an array of prime numbers saved//Notprime is the subscript that holds the number of primes                intPrime[] =New int[limit]; BooleanNotprime[] =New Boolean[Limit];//false By default//true is not a prime number//false is a prime number        intN D W;  for(inti=2;i<limit;++i) {            if(!Notprime[i]) {Prime[p++]=i;  for(intj=i+i;j<limit;j+=i) {Notprime[j]=true; }            }        }        returnPrime; }
View Code

V2.0

Thought:

1.2 is prime

2. Divided by 2 is the number of 0 is not a prime, one filter

3.2 Does not filter out 3, again, divided by 3 is 0 of the filter out

4 has been filtered out in 4.2, and the next number starts at 5,

5. Repeat the process above

Java Program:

    int[] SetPrime2 (intlimit) {        intprime[]=New int[limit]; BooleanIsPrime =true; //true is the prime number//false is not a prime numberPrime[0] = 2; intP =1;  for(intI=3;i<limit;i+=1) {IsPrime=true;  for(intj=0;j<p;j++)                if(i%prime[j]==0) {IsPrime=false;  Break; }            if(isprime==true) Prime[p++] =i; }        returnPrime; }
View Code

V2.1

Two arrays are defined in V1.0, one is an array of prime numbers, and one is an array of prime numbers based on the subscript index

The program can be optimized when only the second array is required in the general case

It's a little hard to say, look at the program.

Just feel the same.

The upper bound limit has been judged by the value near sqrt (limit) Whether it is a prime number

    Boolean[] SetPrime3 (intlimit) {        intPrime[] =New int[limit]; intN D W; intSublimit = (int) math.sqrt (limit) +1; BooleanNotprime[] =New Boolean[limit];  for(inti=2;i<sublimit;++i) {            if(!Notprime[i]) {Prime[p++]=i;  for(intj=i+i;j<limit;j=j+i) {Notprime[j]=true; }                }        }        //Notprime Save all prime number information. True for subscript is not prime, false for subscript is prime//Prime just keeps the prime number within the limit of the square root, noting only primes//There's a lot of similarities here and Setprime.        returnNotprime; }
View Code

All Programs

 PackageCodeforces; Public classFindprime {voidrun () {intlimit=10000; int[] prime =setPrime2 (limit);  for(inti=0;i<limit;i++) {System.out.print (Prime[i]+" "); if(i%50==0) System.out.println (); if(prime[i+1]==0) Break; }    }    Boolean[] SetPrime3 (intlimit) {        intPrime[] =New int[limit]; intp = 0; intSublimit = (int) math.sqrt (limit) +1; BooleanNotprime[] =New Boolean[limit];  for(inti=2;i<sublimit;++i) {            if(!Notprime[i]) {Prime[p++]=i;  for(intj=i+i;j<limit;j=j+i) {Notprime[j]=true; }                }        }        //Notprime Save all prime number information. True for subscript is not prime, false for subscript is prime//Prime just keeps the prime number within the limit of the square root, noting only primes//There's a lot of similarities here and Setprime.        returnNotprime; }    int[] SetPrime2 (intlimit) {        intprime[]=New int[limit]; BooleanIsPrime =true; //true is the prime number//false is not a prime numberPrime[0] = 2; intP =1;  for(intI=3;i<limit;i+=1) {IsPrime=true;  for(intj=0;j<p;j++)                if(i%prime[j]==0) {IsPrime=false;  Break; }            if(isprime==true) Prime[p++] =i; }        returnPrime; }    int[] SetPrime1 (intlimit) {        //Prime is an array of prime numbers saved//Notprime is the subscript that holds the number of primes                intPrime[] =New int[limit]; BooleanNotprime[] =New Boolean[Limit];//false By default//true is not a prime number//false is a prime number        intp = 0;  for(inti=2;i<limit;++i) {            if(!Notprime[i]) {Prime[p++]=i;  for(intj=i+i;j<limit;j+=i) {Notprime[j]=true; }            }        }        returnPrime; }         Public Static voidMain (string[] args) {NewFindprime (). run (); }}
View Code

To find prime numbers by filtering method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.