Most pythonic way avoiding for loops to find the largest number multiple of n but lower than an upperbound x?
Practical example:
n = 48
x = 2636
48 * 54 = 2592 is the nearest.
I am doing a for loop till I don't go over x now
The simplest way is probably using //:
(x // n) * n
If the number has to be strictly less than x, use x - 1 instead:
((x - 1) // n) * n
The expression x // n is the floor division of x by n, discarding any remainder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With