Skip to content

Instantly share code, notes, and snippets.

@audrl1010
Last active June 20, 2018 05:48
Show Gist options
  • Save audrl1010/3b3c1d3be4e9b30189a554f1448f923a to your computer and use it in GitHub Desktop.
Save audrl1010/3b3c1d3be4e9b30189a554f1448f923a to your computer and use it in GitHub Desktop.
에라토스테네스의 체
// '에라토스테네스의 체'로 n개의 범위의 소수들을 구하는 방법.
public int[] primes(int n) {
  int [] datas = new int[n+1];
  for(int i = 2; i <= n; i++) {
    datas[i] = i;
  }

  for(int i = 2; i <= n; i++) {
    if (datas[i] == 0) {
      continue;
    }
    for(int j = i * 2; j <= n; j += i) {
      // i의 배수를 0으로 바꿈.
      datas[j] = 0;
    }
  }
  return datas;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment