The prime number screening method is a method for quickly finding all the primes in a large integer interval, in which the steps are: to find a multiple of all 2 in the interval (for example, 1~n) except 2, and cross out. Then find the multiples of all 3 except 3, and cross them off. Then 5, 7, ..., continue this method until √n. Why is this method correct? In this case, all primes in the interval cannot contain other prime factors, so the number of all containing elements can be removed from the interval, and the number that is closest to the multiple of the last cross-crossed number must be prime (for example, multiples of 2 4,6,8,10, ...). After being crossed off, the number of the nearest 2 without being crossed out 3 must be prime, which is easily introduced by the arithmetic basic theorem in number theory, and if all the primes are not the factor of the number, then the number is prime. In addition, why filter to √n can stop, because after √n, a number if can be sieved, then it must have been screened out before, this is because N is more than √n factor, then it must have a factor of less than √n, the number is either prime, or composite, the prime words must have been sifted out before, If it is composite, it must break down into the product of a number of primes, so it has been sifted off before. Theory has, immediately practice it, the following topics are taken from hdu_1215:problem DescriptionOn the day of Tanabata, matchmaker came to the digital kingdom, and he put a sign on the city gate and said to the people of the digital kingdom, "Do you want to know who your other half is?" then follow the signs. "People come to the notice and want to know who is the other half." The notice is as follows: the factor of the number n is all positive integers that are smaller than n and divisible by N, as the factor of 12 has 1,2,3,4,6. Do you want to know your other half ?Inputthe first line of the input data is a number T (1<=t<=500000), which indicates the number of groups of test data. Then there is the T-group test data, each set of test data has only one number N (1<=n<=500000).Outputfor each set of test data, output a number that represents the other half of the input data N.Sample Input32Ten -Sample Output18 A An easy-to-think approach is brute force search:
#include <stdio.h>//timed outintMainvoid){ intt, n, sum; scanf ("%d", &t); while(t--) {scanf ("%d", &N); Sum=0; for(intFAC =1; FAC <= n/2; fac++)//The n factor cannot be greater than N/2 { if(n% FAC = =0) Sum+=FAC; } printf ("%d\n", sum); } return 0;}
As the note says, the method times out, so we need to find another way to improve efficiency: it can be enumerated to √n, if N has a factor smaller than it, then there must be a bigger factor.
#include <stdio.h>//Optimized#include <math.h>intMainvoid){ intt, n, sum; scanf ("%d", &t); while(t--) {scanf ("%d", &N); Sum=1; //notation One: for (int FAC = 2; FAC <= floor (sqrt (n) +0.5); fac++) directly times out because each cycle has to be sqrt. //notation Two: int bound = floor (sqrt (n) +0.5); //for (int fac = 2; FAC <= bound; fac++)//452MS. //Three://280MS. In contrast, the SQRT function is too time-consuming. You can use multiplication instead of sqrt. for(intFAC =2; FAC*FAC <= N; Fac++) { if(n% FAC = =0) {sum+=FAC; if(FAC*FAC! =N) Sum+ = n/FAC; }} printf ("%d\n", sum); } return 0;}
Well? What about the prime sieve? Is it useless? Why don't I introduce it:----Let's try it:
#include <stdio.h>//Prime Sieve, 405MS#include <math.h>#include<string.h>#defineMAXN 500000intprime[maxn+Ten];intMainvoid) {memset (prime,0,sizeof(prime)); //constructs 1-500000 All primes, 0 is prime, 1 is not prime for(inti =2; I*i <= MAXN; i++) if(Prime[i] = =0) for(intj = i+i; J <= Maxn; J + =i) prime[j]=1; intt, n, sum; scanf ("%d", &t); while(t--) {scanf ("%d", &N); Sum=1; if(Prime[n] = =0) ; Else { for(intFAC =2; FAC*FAC <= N; Fac++) { if(n% FAC = =0) {sum+=FAC; if(FAC*FAC! =N) Sum+ = n/FAC; }}} printf ("%d\n", sum); } return 0;}
As you can see, compared to the last program, time is increasing, I can only find the strength of the test is not enough:-
In the careful thinking, this problem is actually the prime sieve method of deformation, prime sieve method is left prime, and we ask for factors and the test is strong enough, the use of prime sieve method can really help us improve the efficiency of the program, but this is only indirectly using the idea of prime sieve method, we can directly apply its ideas, direct to the target. That is, the factor of each number is directly calculated and placed in the array:
#include <stdio.h>//According to the idea of prime sieve, to a tough point, 78MS on AC#include <string.h>#defineMAXN 500000intsum_fac[maxn+Ten];intMainvoid){ for(inti =2; I <= maxn/2; i++)//The MAXN factor cannot exceed MAXN/2 for(intj = i+i; J <= Maxn; j+=i) {sum_fac[j]+ = i;//the number of factors can be obtained by changing I to 1 } intT, N; scanf ("%d", &t); while(t--) {scanf ("%d", &N); printf ("%d\n", sum_fac[n]+1); } return 0;}
Finally, we do not discuss the problem of the existence of the triangle love, narcissism, or to take the test, I tested, for 1 factor and when, according to test instructions should be 0, but the test data does not seem to be 1, because a slight modification of the program, 1 as a special case to deal with, it seems that any value can pass.
Prime number Screening Method