Data Structures & AlgorithmsModerateICPC regional · number theory warm-up
Problem: How many ordered pairs (a, b) with 1 <= a, b <= 6 have gcd(a, b) = 1?
Problem: How many ordered pairs (a, b) with 1 <= a, b <= 6 have gcd(a, b) = 1?
from math import gcd print(sum(1 for a in range(1, 7) for b in range(1, 7) if gcd(a, b) == 1))What is printed?
- A. 21
- B. 25
- C. 23
- D. 18
Correct answer: C. 23
Pairs with gcd above 1 number 13: those with both entries even (9 pairs) plus pairs with a common factor 3 or 5 not already counted. The remaining 23 pairs are coprime.
More Coding Interview Problems questions
- Problem: What does the following print? python print(sum(bin(i).count('1') for i in range(1, 16)))
- Array a = [5, -3, 2, 4, -6, 1, 3, -2]. How many non-empty contiguous subarrays have a sum in the closed range [0, 4]?
- For the string s = "aabbabbaab", find the length of the longest substring with exactly as many letter a as letter b.
- For the array a = [4, 1, 7, 2, 2, 5, 3, 6], how many contiguous non-empty subarrays have XOR equal to 0? Use prefix XOR values.
- Array a = [3, 1, 4, 1, 5, 9, 2, 6]. How many subarrays of length exactly 3 have an even sum?
- Array a = [2, 4, 6, 1, 3, 5, 8, 7]. How many contiguous subarrays of length at least 2 have a sum divisible by 4?