Measure the test taker's knowledge about the linear screening prime number method.

Source: Internet
Author: User

Const int n= 25600000;
Bool a [n];
Int P [N];
Int N;

Void prime1 (){
Memset (A, 0, N * sizeof (A [0]);
Int num = 0, I, J;
For (I = 2; I <n; ++ I) if (! A [I]) {
P [num ++] = I;
For (j = I + I; j <n; j + = I ){
A [J] = 1;
}
}
}

Void prime2 (){
Memset (A, 0, N * sizeof (A [0]);
Int num = 0, I, J;
For (I = 2; I <n; ++ I ){
If (! (A [I]) P [num ++] = I;
For (j = 0; (j <num & I * P [J] <n); ++ J ){
A [I * P [J] = 1;
If (! (I % P [J]) break;
}
}
}

Test:

Prime number in the range of [0, 100000)
First Prime Number screening method 0 ms
Second Prime Number screening method 0 ms

Prime number in the range of [0, 200000)
First Prime Number Screening Method 15 ms
Second Prime Number screening method 0 ms

Prime number in the range of [0, 400000)
First Prime screening method 16 Ms
Second Prime Screening Method 15 ms

Prime number in the range of [0, 800000)
The first prime screening method is 47 Ms
Second Prime screening method 16 Ms

Prime number in the range of [0, 1600000)
First Prime screening method 62 Ms
Second Prime screening method 63 MS

Prime number in the range of [0, 3200000)
The first prime screening method is 297 Ms
Second Prime screening method 109 Ms

Prime number in the range of [0, 6400000)
The first prime screening method is 922 Ms
Second Prime screening method 266 Ms

Prime number in the range of [0, 12800000)
The first prime screening method is 2187 Ms
Second Prime screening method 563 Ms

Prime number in the range of [0, 25600000)
The first prime screening method is 4828 Ms
Second Prime screening method 1187 Ms

Proof: any sum is marked only once.
You can try to execute the process of this program to understand it.

How are you doing?
What, I don't think the program's efficiency is much improved, so it's useless?
Change a [] to the int type, and then
Void prime2 (){
Memset (A, 0, N * sizeof (A [0]);
Int num = 0, I, J;
For (I = 2; I <n; ++ I ){
If (! (A [I]) P [num ++] = I;
For (j = 0; (j <num & I * P [J] <n & (P [J] <= A [I] | A [I] = 0 )); ++ J ){
A [I * P [J] = P [J];
}
}
}
In this way, a [I] records the minimum quality factor of I.
Then, we can break down the number in [0, n )...
O (number of quality factors) for any number of factors decomposition:
Void factor (int x ){
While (A [x]! = 0 ){
Printf ("% d/N", a [x]);
X/= A [x];
}
Printf ("% d/N", X );
}

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.