Counting steps · routine
How many in range?
Write count_between(n, low, high) returning how many of the numbers 1 to n lie between low and high inclusive — without looping over them.
Clip the range to 1…n first; then it is just a subtraction.
- right answers
- arguments left as they should be
- fast enough at scale
- without sum, len
Run adds print(count_between(10, 3, 5)) after your code, to try it.
Stuck on the idea rather than the code? The Two pointers and formulas lesson walks through it.