4

Given the set [10000,70000], in order to calculate the number of terms divisible by 8 I know of two ways to calculate the answer

1st is $$\frac{70000-10000}{8}+1=7501$$

2nd is $$⌊\frac{60001}{8}⌋=7500$$

the fact that the results differ is very peculiar to me, can anyone explain what is going on, and which one is more accurate?

N. F. Taussig
  • 76,571

6 Answers6

6

The problem is that both endpoints of the interval $[10000, 70000]$ are divisible by $8$, while neither endpoint of the interval $[1, 60001]$ is divisible by $8$.

Your first method is correct. It works since both endpoints are divisible by $8$.

In your second method, your interval should start at $0$ and end at $60,000$ so that both endpoints are again divisible by $8$. You then have to add $1$ to account for the fact that $0$ is divisible by $8$. Observe that $$\left\lfloor \frac{60000 - 0}{8} \right\rfloor + 1 = 7501$$

N. F. Taussig
  • 76,571
5

If you want a catch-all formula for the number of multiples of $n>0$ in $[a,b]$, you may use $$\left\lfloor \frac bn\right\rfloor-\left\lceil\frac an\right\rceil+1,$$

which essentially comes from the consideration that the map $x\mapsto nx$ defined on the integers in $\left[\left\lceil\frac an\right\rceil,\,\left\lfloor \frac bn\right\rfloor\right]$ is bijective onto the set of multiples of $n$ in $[a,b]$. In a number of instances the nuance of where to divide and where to take floor or ceiling is obscured by other facts.

3

Your second method, generalized to the interval $[a,b],$ appears to be $$\left\lfloor \frac{b - a + 1}8\right\rfloor.$$

The floor function gives you an integer, which you want, but it does this simply by throwing out any fractional part. When you use a formula like $$\left\lfloor \frac x8\right\rfloor,$$ you get zero for the first seven positive values of $x$. You don't get a positive result until $x \geq 8.$

Consider the simpler problem of counting multiples of $8$ in the set $[10000, 10000].$ There is one element in that set and it is a multiple of $8$, so the answer should be $1,$ but $$\left\lfloor \frac{10000 - 10000 + 1}8\right\rfloor = \left\lfloor \frac 18\right\rfloor = 0.$$ You'll get the same wrong answer for $[10000,10001],$ $[10000,10002],$ $[10000,10003],$ $[10000,10004],$ $[10000,10005],$ and $[10000,10006].$

For $[10000,10007],$ you get $$\left\lfloor \frac{10007- 10000 + 1}8\right\rfloor = \left\lfloor \frac 88\right\rfloor = 1,$$ which is finally a correct answer. But for $[10000,10008]$ you still get only $1$ from the formula while the correct answer is $2.$

The problem is that your formula only counts every eighth integer, but your interval has a multiple of $8$ at the first integer. The only time the formula is correct is just before you reach the next multiple of $8,$ because that's the only time you can partition the set of integers into subsets with exactly $8$ integers in each subset and exactly one multiple of $8$ in each subset.

If your set started at $10001$ instead of $10000$ the formula would work fine, because the first seven integers starting at $10001$ are not divisible by $8$ and you only get a multiple of $8$ at the eighth integer added to the set.

The catch is that in general, in order to know how many multiples of some integer $n$ are in a set of consecutive integers it is not enough to know how many integers are in the set. The only time this information is sufficient is when the number of integers is an exact multiple of $n$. In any other case you need to know where the sequence starts (at least relative to the nearby multiples of $n$) in order to know whether there is an "extra" multiple of $n$ in the numbers that are left over after you have partitioned the rest of the numbers into subsequences of $n$ numbers each.

David K
  • 98,388
  • So basically, if my minimum and maximum values are divisible by 8, I could use the floor formula, and add an extra 1 to it and it should get the right result? – MathCurious Dec 26 '20 at 07:37
  • @MathCurious It would get the correct result, but if you happen to know both endpoints are divisible by $8$, the first formula is simpler to explain. – David K Dec 26 '20 at 14:19
2

Hint: This is where those "flooring methods" came from: $$10,000 \le 8k \le 70,000 \\[1mm] \frac{10,000}{8} \le k \le \frac{70,000}{8} \\[1mm] \\ 1250 \le k \le 8750 $$ Now, just count how many natural numbers there are between $1250$ and $8750$; that is, $8750 - 1249 = 7501$.


Since, the ratios above might not be integer in general, we can sharpen the bounds $$\left\lceil\frac{10,000}{8}\right\rceil \le k \le \left\lfloor\frac{70,000}{8}\right\rfloor $$ which shows that your first approach is right.

VIVID
  • 11,604
  • 1
    Hi thanks for the response! :) But the answer to the problem is already in the question, and the actual question is which method is more accurate. – MathCurious Dec 25 '20 at 16:52
  • @MathCurious Actually, this is where those "flooring methods" came from – VIVID Dec 25 '20 at 16:54
0

Or, using the A.P. formula one can also derive the answer.

Say, for a given range [a,b], we want to find out the numbers in that range that are divisible, say by some number 'k'.

Procedure: Find out the first smallest multiple of k in that range, then find out the last multiple of k in that same range. Then, just calculate the number of terms starting from the first multiple till the last multiple (all of which must lie in that given range), using the A.P. formula for finding the $n^{th}$ term.

Example: Given range $[6, 498]$. We want to list out all the numbers in that range that are divisible by $7$. The smallest multiple of $7$ (in that range) is $7$, and the largest is $497$. The number of terms starting from $7$ till $497$ (inclusive) are:

$$497=7+(n-1)*7\Rightarrow n=71.$$

0

Numbers in the interval [10000, 70000] are the numbers from 1 to 70000, minus the numbers from 1 to 9999. So whatever you want to count, you count from 1 to 70000 and subtract the count from 1 to 9999.

In this case, the count of numbers from 1 to n divisible by k is n/k, rounded down to the nearest integer. So 70000/8 rounded down, minus 9999/8 rounded down.

gnasher729
  • 10,113