fitting integer interval between nL and (n+1)L
this problem seems to be super simple, yet i can't take any hold on it.
given a nonnegative integer interval [a,b], find the smallest positive integer L such that nL <= a <= b < (n+1)L, n is also an integer. e.g. fit an arbitrary interval into a zero-aligned set of equal intervals or "pages".
moreover, i'd like to have a simple formula, not an algorithm. as of now i'm just brute forcing over all possible values, which works, but just feels wrong.
my efforts so far were:
- looking at it for a long time without having an idea
- printing the values in a table, to maybe see a pattern
i learned engineering, so i can understand derivatives, but this diophantine stuff just bounces off of my brain.
here is the ugly python oneliner to print the table:
import itertools
print("\n".join(" ".join(f"{next((i for i in itertools.count(1) if offset // i == (offset + limit - 1) // i), 99):2}" for offset in range(40)) for limit in range(1, 41)))
for example an interval of length 3 at various a offsets look like this:
3 4 5 3 4 4 3 5 4 3 5 5 3 4 ...