Code RoomCount coprime pairs
HardPrep Room Coding #754

Count coprime pairs

CodingAlgorithms & data structuresSenior–Staff~30 min

Given an integer n (1 <= n <= 100000), count the number of unordered pairs (i, j) with 1 <= i < j <= n such that gcd(i, j) == 1 (i and j are coprime). For example, for n = 5 there are 9 coprime pairs. A naive O(n^2) gcd over all pairs is too slow at the upper bound.

Implement
count_coprime_pairs(n: int) → int
Examples
in[5]out9
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[5]9not run yetsample