Why is "i-r" ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

28th Jan 2021, 10:56 AM
inquiorx
inquiorx - avatar
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)
28th Jan 2021, 11:16 AM
David Ashton
David Ashton - avatar
+ 6
print(max(i for i in range(100) if i % 22 == 0)) is simpler
28th Jan 2021, 11:09 AM
David Ashton
David Ashton - avatar
+ 5
99 - 11 = 88 its a minus
28th Jan 2021, 11:04 AM
Slick
Slick - avatar
+ 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.
28th Jan 2021, 11:20 AM
jtrh
jtrh - avatar
+ 1
print(max(I for i in range(100)if i % 22==)) I think it is right
29th Jan 2021, 11:50 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar