Develop
2013.04.23 14:07
[java] Sieve of Eratosthenes (에라토스테네스의 체)
조회 수 9353 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
» | Develop | [java] Sieve of Eratosthenes (에라토스테네스의 체) | hooni | 2013.04.23 | 9353 |
588 | Develop | [js] 사진첩에 쓸 내용 - 마우스 오버로 바꾸기 | hooni | 2013.04.23 | 6364 |
587 | Develop | [js] 폼(form) 전송시 중복 클릭 방지 간단한 구문 | hooni | 2013.04.23 | 9330 |
586 | Develop | [ajax] 샘플 코드와 한글처리에 대한 간단한 설명 | hooni | 2013.04.23 | 6877 |
585 | Develop | [c] 전위 표기법으로 연산 예제.. | hooni | 2013.04.23 | 9325 |
584 | Develop | [c] 프로그램 코드(c/c++)를 html 파일로 변환 | hooni | 2013.04.23 | 7697 |
583 | Develop | [c] 구조체 배열 예제 (학생 성적 계산) | hooni | 2013.04.23 | 7691 |
582 | Develop | [c] 숫자 맞추는 게임.. | hooni | 2013.04.23 | 6954 |
581 | Develop | [js] php의 number_format() 함수와 같은.. ㅋㅋ | hooni | 2013.04.23 | 6955 |
580 | Develop | [c] 베이지언(Bayesian) 패턴인식 과제 ㅋㅋ | hooni | 2013.04.23 | 7119 |
579 | Develop | [c] KNN(K-nearest neighbor) 패턴인식 과제.. | hooni | 2013.04.23 | 7417 |
578 | Develop | [php] 날짜 정보 출력 (년,월,일,시,분,초) | hooni | 2013.04.23 | 10584 |