Skip to content

Commit

Permalink
solve(BOJ): G5_1456_거의_소수_kt
Browse files Browse the repository at this point in the history
  • Loading branch information
gogumaC committed Jan 14, 2024
1 parent ffd815c commit 9ed39d0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/boj/G5_1456_거의_소수.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package boj

import kotlin.math.pow
import kotlin.math.sqrt

class BOJ1456() {


fun solve() {
val (a, b) = readln().split(" ").map { it.toLong() }

val MAX = sqrt(b.toDouble()).toInt()
val isPrime = BooleanArray(MAX + 1) { true }
var count = 0
isPrime[0] = false
isPrime[1] = false

for (i in 2..MAX / 2) {
if (!isPrime[i]) continue
var mul = 2
while (true) {
isPrime[i * mul] = false
mul++
if (i * mul > MAX) break
}
}

for (i in 2..MAX) {
if (!isPrime[i]) continue
var exp = 2
while (true) {
val num = i.toDouble().pow(exp)
if (num >= a && num <= b) count++
else if (num > b) break
exp++
}
}

println(count)
}
}

fun main() {
BOJ1456().solve()
}

0 comments on commit 9ed39d0

Please sign in to comment.