Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|cite|improve this question
this problem just might be NP-complete. i hope you have enough computing power to tackle it. – Justin L. Dec 8 '10 at 22:54
up vote 5 down vote accepted

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.

share|cite|improve this answer
@Cudos: and if your quantities are both integers, and you include all integers between the two in your count, and only integers... – Arturo Magidin Dec 8 '10 at 21:23
@Arturo: Yes, that was the assumption made based on the example. – Aryabhata Dec 8 '10 at 21:25
Not faulting you; I always have to hit my students over the head with the fact that "numbers" doesn't mean "integers and only integers" unless one says so explicitly. – Arturo Magidin Dec 8 '10 at 21:26
@Arturo: Agree with that :-) – Aryabhata Dec 8 '10 at 21:28

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.

share|cite|improve this answer

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

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.