Count number of values between two numbers

$\begingroup$

I am finding with some formulas for my web application. I need a formula that counts the number of values between two number, e.g. there are four numbers (3,4,5,6) between 2 and 7

$\endgroup$ 1

4 Answers

$\begingroup$

larger - smaller - 1. If you don't count the larger and the smaller (like your example).

If the numbers are $n, n+1, \dots, n+k , n+k+1$, the numbers you want are $n+1, n+2, \dots, n+k$, which are $k$ in number = $n+k+1 - n - 1$ = larger - smaller - 1.

$\endgroup$ 4 $\begingroup$

The number between $a,b$ is $(a<b)$

  • Including $a,b$ them self: $b-a+1$
  • Excluding $a,b$ :$b-a-1$
$\endgroup$ $\begingroup$

HINT $\ $ The problem admits an obvious shift symmetry, e.g. considering the example that you gave, a shift of $-2\:$ to $[3, 6]$ yields $[1,4]$, which clearly has $4$ integers. Innate symmetries should always be investigated since they may play a key role in simplifying the search for a solution. Moreover, it's best to learn these symmetry-based problem solving techniques for simple problems like this, since you may not be able to see the forest for the trees in much more complex problems.

$\endgroup$ $\begingroup$

The bigger number minus the smaller number minus 1. Or if $b$ is the bigger number, $s$ the smaller. We have: $b - s - 1$

$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like