Develop
2013.04.23 14:07
[java] Sieve of Eratosthenes (에라토스테네스의 체)
조회 수 9337 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
789 | Develop | [c] 문자열 자르는 함수(strtok) 예제 | hooni | 2013.04.23 | 12176 |
788 | Develop | SVN 명령어 (SVN command) | hooni | 2014.02.28 | 12139 |
787 | Database | [mysql] DB->Text, Text->DB 변환 | hooni | 2003.04.23 | 12129 |
786 | Database | [oracle] SQL문 실행 방법 3가지 | hooni | 2013.12.19 | 12123 |
785 | Develop | 참고하고 지울 자료.. 집에서 바야지.. ㅋㅋ | hooni | 2013.04.23 | 12123 |
784 | Develop | [ios] GCD 변수 사용 예제 | hooni | 2013.10.01 | 12110 |
783 | System/OS | [linux] 데스크탑환경(GNOME/KDE) 바꾸기.. | hooni | 2003.04.23 | 12101 |
782 | PPT | [ppt] Magic URLs & Hidden Form Fields 에 대해.. ㅋㅋ | hooni | 2013.04.23 | 12093 |
781 | Develop | [c++] 기초강좌 #03(함수의 매개변수 전달) | hooni | 2003.04.23 | 12082 |
780 | System/OS | [linux] /etc/fstab 설정 방법.. ㅋㅋ | hooni | 2013.04.23 | 12081 |
779 | Develop | [c++] 현승이가 준 P2P 프로그램 소스 ㅋㅋ | hooni | 2013.04.23 | 12066 |
778 | System/OS | [linux] 쉘 스크립트 (Shell Script) | hooni | 2003.04.23 | 12064 |