[Mathematics] 공간복잡도를 줄인 에라토스테네스의 체를 활용한 소수 산출

Prime Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <memory.h>
#define MAXNUM 1000000

bool is_Prime[500001]; //(MAXNUM/2) + 1

int main(int argc, char** argv){
register int i;
memset(is_Prime,true,sizeof(is_Prime));
is_Prime[0] = false;
for(int i=3; i*i<=MAXNUM;i+=2){
if(is_Prime[i>>1]){
for(int j = i*i; j<=MAXNUM; j += (i<<1))
is_Prime[j>>1] = false;
}
}
printf("Prime : 2\n");
for(i=3;i<=MAXNUM; i+=2){
if(is_Prime[i>>1])
printf("Prime : %d\n",i);
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/21/Algorithm/primeNumber/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.