Predict the output
What does this print?
n = 10
count = 0
for outer in range(n):
for _ in range(outer):
count += 1
print(count, n * (n - 1) // 2)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 45 45
Right: 0 + 1 + … + 9 = 45, which is n(n - 1)/2.
Half of n² is still quadratic, so a dependent inner loop does not change the Big O class.
Why not the others?
- B 100 45. The inner loop runs i times, not n times.
- C 55 45. range(i) stops before i, so the largest inner count is 9.