Skip to Content

Prime ranges

Level B. Teaches std.math.Sieve and prefix counts.

You are given q queries (1 ≤ q ≤ 2·10⁵), each two numbers 1 ≤ l ≤ r ≤ 10⁵. For each, print how many primes lie between l and r, inclusive.

Input: q, then q lines l r. Output: one line per query.

InputOutput
2
1 10
10 20
4
4
5 tests · Ctrl+Enter
Hints
  • Sieve(N) from std.math builds the table once, in O(N log log N). sv.is_prime(k) is then a lookup.
  • Prefix(...) builds the prefix sums once, above the queries, and pre.fold(l..=r) is the number of primes in l..=r in O(1). Writing the pre[k] = pre[k - 1] + ... loop yourself is what the hand_rolled_prefix_sum lint flags.
  • Sieve and Prefix are in the prelude, so no use is needed; the math reference and the library guide list the rest.