Data Structures & AlgorithmsVery hardICPC regional · number theory

Problem: What is the sum of Euler's totient phi(k) for k = 1 to 20? Run the code.

Problem: What is the sum of Euler's totient phi(k) for k = 1 to 20? Run the code.
from math import gcd
print(sum(sum(1 for g in range(1, k + 1) if gcd(g, k) == 1) for k in range(1, 21)))
What is printed?
  1. A. 128
  2. B. 110
  3. C. 120
  4. D. 100

Correct answer: A. 128

Summing the totient phi(k) for k = 1..20 gives 128, computed exactly by counting coprime residues for each k.

More Coding Interview Problems questions

Exams that ask this topic