The Off-by-One Loop
def count_something(n):
total = 0
for i in range(n):
for j in range(i, n):
if (i + j) % 2 == 0:
total += 1
return total
print(count_something(10))
Write out the first few iterations by hand — the pattern emerges quickly.
Comments
Loading the conversation…