Skip to content

Commit

Permalink
백준 1456 거의 소수 java solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mong3125 committed Apr 10, 2024
1 parent 95b4a96 commit 22d1049
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions mong3125/정수론/BOJ1456_거의소수구하기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package 정수론;

import java.util.Arrays;
import java.util.Scanner;

public class BOJ1456_거의소수구하기 {
static long A, B;
static boolean[] isPrime;
public static void main(String[] args) {
// 입력
Scanner sc = new Scanner(System.in);
A = sc.nextLong();
B = sc.nextLong();
isPrime = new boolean[10000001];

Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;

for (int i = 2; i < 10000001; i++) {
if (!isPrime[i]) continue;

for (int j = i+i; j < 10000001; j += i) {
isPrime[j] = false;
}
}

int count = 0;
int max = (int) Math.min(10000001, Math.sqrt(B));
for (int i = 2; i < max; i++) {
if (isPrime[i]) {
long now = i;
while ((double) i <= (double) B / (double) now) {
if ((double) i >= (double) A / (double) now) count += 1;
now *= i;
}
}
}

System.out.println(count);
}
}

0 comments on commit 22d1049

Please sign in to comment.