Data Structures & AlgorithmsHardCodeforces Div2 C/D · Amazon SDE OA

Compute the sum of gcd(i, j) over all pairs 1 ≤ i < j ≤ 50.

Compute the sum of gcd(i, j) over all pairs 1 ≤ i < j ≤ 50.
from math import gcd
print(sum(gcd(i,j) for i in range(1,51) for j in range(i+1,51)))
What is printed?
  1. A. 2725
  2. B. 4000
  3. C. 592654
  4. D. 2676

Correct answer: A. 2725

Iterate over the unordered pairs and add their gcd; including i = j adds the diagonal, using lcm sums a different function, and skipping j = i+1 drops whole pairs.

More Coding Interview Problems questions

Exams that ask this topic