Develop
2013.04.23 14:07
[java] Sieve of Eratosthenes (에라토스테네스의 체)
조회 수 9331 댓글 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
-
[c] 거리와 각도를 입력받아서 좌표로 변환
-
[c++] 퍼즐 버블버블 간단한 원리(사용공식)
-
[c] 문자열 자르는 함수(strtok) 예제
-
[ms-sql] 프로시져 예제..
-
객체지향 프로그래밍에 대한 개념.. (객체)
-
[jsp] 초간단한 include 구문 예제..
-
[unix] AIX쉘 초기 파일 (.cshrc)
-
[php] 데이터를 엑셀,워드 형태로 변환할 때 헤더(ms-office)
-
[jsp] 페이지에 한글이 정상적으로 보이지 않을 때..
-
[asp] 기본 문법과 제어문
-
[asp] 문자열 관련 함수 요약
-
[asp] 문자열 넘겨받기 (get,post)