[JAVA/실습] N번째 소수 구하기.
Q.
소수를 크기 순으로 나열하면 2, 3, 5, 7, 11, 13, ... 과 같이 됩니다.
이 때 10,001번째의 소수를 구하시오. >> 이거는 소수일 경우 카운트를 해야되는데 이부분을 잘 못하겠네요 ㅠㅠ
public class Quest2 { public static void main(String[] args) { int value = 1000; int count = 0; int sosuCount = 0;
for (int i = 1; i <= value; i++) {
for (int j = 1; j <= i; j++) { if (i % j == 0) { count++; } }
if (count > 2) { System.out.println(i + "소수가 아닙니다."); } else { System.out.println(i + "소수 입니다."); sosuCount++; }
count = 0;
if (sosuCount == 10001) { System.out.println("10001번째 소수는 " + i); } else { if (value - i < 10) { value += 1000; } }
}
} }
|