0
Why is "i-r" ?
Here is the code: m = 0 for i in range(100): r = i%22 m = max(m, i-r) print(m) I feel like it means to count the max x %22==0 and the answer is 88 indeed. But I still can't figure out why here is "i-r" yes, i is 99, r is 11, but why?
5 Answers
+ 5
You can insert print() functions to show what's going on
m = 0
for i in range(100):
print("i =", i)
r = i%22
print("r =", r)
m = max(m, i-r)
print("i - r =", i - r)
print("m =", m)
print(m)
+ 6
print(max(i for i in range(100) if i % 22 == 0)) is simpler
+ 5
99 - 11 = 88
its a minus
+ 2
Yes, it prints the largest multiple of 22 that is less than 100.
i - r = i - i % 22
i - i % 22 gives you the largest multiple of 22 that is less than i, just like math.floor(i / 22) * 22.
The intuition behind it is that the remainder i % 22 is "how many steps do you have to go down from i to reach a multiple of 22". Let's say i = 67. We know that 66 is the largest multiple of 22 that is less than 67. In order to go from 67 to 66 you subtract 1. 67 % 22 happens to be 1.
+ 1
print(max(I for i in range(100)if i % 22==))
I think it is right