Data Structures & AlgorithmsModerateCodeforces Div2 A · summation
Problem: What is the sum of i (5 - i) for i from 1 to 4? Run the code.
Problem: What is the sum of i * (5 - i) for i from 1 to 4? Run the code.
print(sum(i * (5 - i) for i in range(1, 5)))What is printed?
- A. 30
- B. 15
- C. 24
- D. 20
Correct answer: D. 20
The terms are 4, 6, 6, 4, which sum to 20. Miscounting one term (for example using 6 for i = 1) gives a wrong total.
More Coding Interview Problems questions
- Problem: What is the sum of gcd(i, j) over all pairs 1 <= i < j <= 4? Run the code. python from math import gcd print(sum(gcd(i, j) for i…
- Problem: What is the digit sum of 2^100? Run the code. python print(sum(int(c) for c in str(2 100))) What is printed?
- Problem: How many triples (a, b, c) with 1 <= a <= b <= c <= 10 have a + b + c = 12? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the largest prime factor of 600851475143? Run the trial-division code. python n = 600851475143 f = 2 last = 1 while f f…
- Problem: How many integers in [1, 999] are divisible by 7 but not by 14? Run the code. python print(sum(1 for x in range(1, 1000) if x % 7…
- Problem: How many ordered pairs (i < j) of [1, 2, 3, 4, 6] have a[j] divisible by a[i]? Run the code. python a = [1, 2, 3, 4, 6]…