Develop
2013.04.23 14:07
[java] Sieve of Eratosthenes (에라토스테네스의 체)
조회 수 9345 댓글 0
소수를 구하는 또다른 방법입니다.
러닝타임은 N + N/2 + N/3 + N/5 + N/7 + N/11 + ....
class Primes {
public static void main(String[] args) {
int num = 100;
if (args.length > 0)
num = Integer.parseInt(args[0]);
getPrime(num);
}
public static void getPrime(int max) {
boolean[] a = new boolean[max];
for (int i = 2; i < max; i++)
a[i] = true;
int to = (int)Math.sqrt(max);
for (int i = 2; i < to; i++)
if (a[i] != false)
for (int j = i; j*i < max; j++)
a[i*j] = false;
for (int i = 2; i < max; i++)
if (a[i])
System.out.print(" " + i);
System.out.println();
}
}
[출처] http://blog.naver.com/charityno3?Redirect=Log&logNo=80006915007
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
277 | Develop | [c] 문자열 정렬 함수 qsort() | hooni | 2003.04.23 | 8261 |
276 | Develop | [c] 문자열 자르는 함수(strtok) 예제 | hooni | 2013.04.23 | 12196 |
275 | Develop |
[c] 문자열 라이브러리 최신버전
![]() |
hooni | 2003.04.23 | 7215 |
274 | Develop | [c] 문자열 뒤집기 초간단 샘플 코드 ㅎㅎ | hooni | 2013.04.23 | 7130 |
273 | Develop | [c] 문자열 뒤집기 (문자열 거꾸로 출력하는 간단소스) | hooni | 2003.04.23 | 10033 |
272 | Develop |
[c] 문자열 str_shift 예제..
![]() |
hooni | 2013.04.23 | 6848 |
271 | Develop |
[c] 무선 Radius Server 자료..
![]() |
hooni | 2013.04.23 | 7278 |
270 | Develop | [c] 메시지큐(Message Queue) 설명.. (joinc) | hooni | 2013.04.23 | 14293 |
269 | Develop |
[c] 메세지 프로그램 (Server - Agent - Client)
![]() |
hooni | 2013.04.23 | 6477 |
268 | Develop |
[c] 맵서치인 듯(옛날 컴에서 찾은 자료)
![]() |
hooni | 2013.04.23 | 6912 |
267 | Develop |
[c] 마우스 따라다니는 고양이 - 네코95 (WinAPI)
![]() |
hooni | 2013.04.23 | 7953 |
266 | Develop |
[c] 로또(Lotto) 번호 생성기
![]() |
hooni | 2013.04.23 | 7179 |