소수 구하기 예제 - 2
Last updated
Last updated
min(시작 수) max(종료 수)
A(소수 리스트)
for 2 ~ 10,000,000: # 10^14의 제곱근까지만
A 리스트 초기화
for 10,000,000의 제곱근까지:
소수가 아니면 넘어감
for 소수의 배숫값을 10,000,000까지:
현재 수가 소수가 아니라는 것을 표시
for 2 ~ 10,000,000:
A 리스트에서 소수인 값일 때:
temp(현재 소수)
while 현재 소수 <= max/tempimport math
min, max = map(int, input().split())
A = [0] * 10_000_001
for i in range(2, len(A)):
A[i] = i
for i in range(2, int(math.sqrt(len(A)) + 1)):
if A[i] != 0:
for j in range(i * 2, len(A), i):
A[j] = 0
count = 0
for i in range(2, 10_000_001):
if A[i] != 0:
temp = A[i]
while A[i] <= max / temp:
if A[i] >= min / temp:
count += 1
temp *= A[i]
print(count)import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
long min = Long.parseLong(st.nextToken());
long max = Long.parseLong(st.nextToken());
int[] A = new int[10_000_001];
for (int i = 2; i < A.length; i++) {
A[i] = i;
}
for (int i = 2; i <= Math.sqrt(A.length); i++) {
if (A[i] != 0) {
for (int j = i * 2; j < A.length; j += i) {
A[j] = 0;
}
}
}
int count = 0;
for (int i = 2; i < A.length; i++) {
if (A[i] != 0) {
long temp = A[i];
while (A[i] <= (double)max / (double)temp) {
if (A[i] >= (double)min / (double)temp) {
count++;
}
temp *= A[i];
}
}
}
System.out.println(count);
}
}