Look at the empty blog do not know what to write, write some previous experience it
The prime algorithm compares the common use, this time writes it.
Here I'm giving my usual notation.
Notation 1:
intn=10000; intprime[10000];//used to store prime numbers intnum=0; intJ; for(intI=2; i<=n;i++){ for(j=2; j<=sqrt (i); j + +) {//The inner layer just loops to sqrt (i). if(i%j==0) Break; } if(J>sqrt (i)) prime[num++]=i;
There are not too many good places to talk about, on these lines, the idea is very simple, just the most basic inner loop j<=n replaced with J<=sqrt[i], small optimized a bit
Notation 2: Prime sieve
int num=0;
int n=10000;
int prime[10000]; //For storing prime numbers
int flag[10005]; //To determine if I is a prime number
memset (flag,0,sizeof (flag));
for (int i=2;i<=n;i++) {
if (flag[i]==0) {
prime[num++]=i;
for (int j = i+i; j<=n; j+=i)
flag[j]=1;
}
}
A very common algorithm, the idea is very simple, each found a prime number, the multiplier of its sieve off (because it is certainly not a prime number), so that time complexity is reduced to linear, so also called the linear prime sieve method
Prime number algorithm