import java.io.PrintWriter import java.util.* import kotlin.math.* fun floorSqrt(n: Long): Long { if (n <= 1) return n var lo = 1L var hi = n while (hi - lo > 1) { val mid = lo + (hi - lo) / 2 val sq = mid.toBigInteger().let { it * it } if (sq <= n.toBigInteger()) lo = mid else hi = mid } return lo } fun primePi(n: Long): Long { if (n <= 3) return maxOf(0, n - 1) val v = floorSqrt(n).toInt() val smalls = (0..v).map { (it + 1) / 2 }.toMutableList() var s = (v + 1) / 2 val roughs = (0 until s).map { 2 * it + 1 }.toMutableList() val larges = (0 until s).map { (n / (2 * it + 1) + 1) / 2 }.toMutableList() val skip = BooleanArray(v + 1) { false } var pc = 0 for (p in 3..v step 2) { if (skip[p]) continue val q = p * p.toLong() pc++ if (q * q > n) break skip[p] = true for (i in q..v step 2L * p) { skip[i.toInt()] = true } var ns = 0 for (k in 0 until s) { val i = roughs[k] if (skip[i]) continue val d = i * p.toLong() val x = if (d <= v) larges[smalls[d.toInt()] - pc] else smalls[(n / d).toInt()].toLong() larges[ns] = larges[k] + pc - x roughs[ns] = i ns++ } s = ns var i = v for (j in (p..v / p).reversed()) { val c = smalls[j] - pc val e = j * p.toLong() while (i >= e) { smalls[i] -= c i-- } } } var res = larges[0] + (s + 2L * (pc - 1)) * (s - 1L) / 2 - larges.slice(1 until s).sum() for (l in 1 until s) { val q = roughs[l] val m = n / q val e = smalls[(m / q).toInt()] - pc if (e <= l) break val t = roughs.slice(l + 1..e).map { smalls[(m / it).toInt()].toLong() }.sum() res += t - (e - l) * (pc + l - 1L) } return res } fun PrintWriter.solve() { val l = nextLong() val r = nextLong() println(primePi(r) - primePi(l - 1) + primePi(2 * r) - primePi(2 * l)) } fun main() { val writer = PrintWriter(System.out, false) writer.solve() writer.flush() } // region Scanner private var st = StringTokenizer("") private val br = System.`in`.bufferedReader() fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() // endregion