Skip to content
Snippets Groups Projects
Commit f194d489 authored by tracer-nju's avatar tracer-nju Committed by guo zling
Browse files

Static hostname: starker-HYM-WXX

       Icon name: computer-laptop
         Chassis: laptop
      Machine ID: 9f936e1bf87b4e7cb0e53e5422bcaf22
         Boot ID: 7f7f08a039ed40d2b473aed355f1e17b
Operating System: Ubuntu 22.04.2 LTS
          Kernel: Linux 5.19.0-45-generic
    Architecture: x86-64
 Hardware Vendor: HONOR
  Hardware Model: HYM-WXX
 11:24:20 up  1:20,  1 user,  load average: 0.52, 0.64, 0.82
parent fa5a352e
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,8 @@ static int primes[N];
int *sieve(int n) {
assert(n + 1 < N);
int *p = primes; *p++ = 2;// primelist ini
is_prime[1] = true; is_prime[2] = true; is_prime[3] = true; //ini
//ini tobe improved
......@@ -22,20 +24,24 @@ int *sieve(int n) {
is_prime[j] = false;
//use other primes
int pi;
for (int i = 3; i <= n; i+=2) {
if(!is_prime[i]) continue; //use prime i only
if(i*i > n) break;
//到这里确定是质数
*p++ = i;
//end
if(i*i > n) {pi = i; break;}
//枚举优化 todo
for(int j = i*i; j<=n; j = j+i+i){
is_prime[j] = false;
}
}
int *p = primes;
for (int i = 2; i <= n; i++)
for (int i = pi; i <= n; i+=2) //减少质数判断的访存。//这里还需要处理大于根号级别的质数
if (is_prime[i]) {
*p++ = i;
}
*p = 0;
return primes;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment