Ma · Mathematics

How to Count Integers Not Divisible by 5

Published By the 9to5Science editorial team

At a glance

  1. 1. Count the total

    Find how many integers are in the specified range.

  2. 2. Count multiples of 5

    Use the fact that an integer is divisible by 5 exactly when it ends in 0 or 5.

  3. 3. Subtract

    Subtract the number of multiples from the total.

  4. 4. Check the endpoints

    Make sure the range includes exactly the intended first and last integers.

To count integers that are not divisible by 5, use the complement principle: count all the integers in the range, count the multiples of 5, and subtract. This works for any finite interval. If the question instead asks you to build numbers from digits, use the same divisibility rule by focusing on the final digit.

The complement method

An integer is divisible by 5 if and only if its final digit is 0 or 5. Thus, among a specified set of integers, the ones divisible by 5 form the part to exclude.

For the integers from 1 through a positive integer NN, there are NN numbers in total. The multiples of 5 are 5,10,15,…,5⌊N/5⌋5,10,15,\ldots,5\lfloor N/5\rfloor, so there are ⌊N/5⌋\lfloor N/5\rfloor of them. Therefore,

count not divisible by 5 from 1 to N=N−⌊N5⌋.\text{count not divisible by 5 from 1 to }N = N-\left\lfloor\frac{N}{5}\right\rfloor.

Here, ⌊x⌋\lfloor x\rfloor means the greatest integer less than or equal to xx. It accounts for the fact that the last multiple of 5 may be below NN.

A worked example: integers from 1 to 100

There are 100 integers from 1 through 100. The multiples of 5 in this range are 5,10,…,1005,10,\ldots,100, or 100/5=20100/5=20 numbers. Subtracting gives

100−20=80.100-20=80.

So 80 integers from 1 through 100 are not divisible by 5.

This also matches the last-digit rule: in each block of ten consecutive integers, two numbers end in 0 or 5, leaving eight that do not. Ten such blocks contain 10⋅8=8010\cdot 8=80 numbers.

Counting in an arbitrary interval

Suppose you want to count integers from aa through bb, including both endpoints, where a≤ba\le b. First count the integers in the interval: there are b−a+1b-a+1. The number of multiples of 5 in that interval is

⌊b5⌋−⌊a−15⌋.\left\lfloor\frac{b}{5}\right\rfloor- \left\lfloor\frac{a-1}{5}\right\rfloor.

Subtract that count from the total:

(b−a+1)−(⌊b5⌋−⌊a−15⌋).(b-a+1)-\left(\left\lfloor\frac{b}{5}\right\rfloor- \left\lfloor\frac{a-1}{5}\right\rfloor\right).

For example, from 37 through 123 there are 123−37+1=87123-37+1=87 integers. The multiples of 5 are counted by

⌊1235⌋−⌊365⌋=24−7=17.\left\lfloor\frac{123}{5}\right\rfloor- \left\lfloor\frac{36}{5}\right\rfloor=24-7=17.

Therefore, 87−17=7087-17=70 integers in that interval are not divisible by 5.

The floor formula also works for negative integer endpoints. For example, it counts multiples such as −10-10 and −5-5 as divisible by 5. If your range includes zero, remember that zero is divisible by 5, so it belongs among the numbers being subtracted.

If you are forming numbers from digits

Sometimes the task is not to count every integer in a consecutive range, but to form numbers using a set of digits. In that case, the final digit determines divisibility by 5. For example, consider three-digit numbers made from the digits 0 through 9, with repetition allowed. The first digit has 9 choices (1 through 9), the middle digit has 10 choices, and the last digit has 8 choices if the number must not be divisible by 5 (any digit except 0 and 5). The multiplication principle gives

9⋅10⋅8=720.9\cdot 10\cdot 8=720.

So there are 720 such three-digit numbers. If repetition is forbidden, or if the allowed digit set differs, the choices for earlier positions may depend on which digits have already been used. In that case, count valid choices position by position and avoid treating dependent choices as independent.

Common mistakes

  • Dividing by 5 without checking the range. The number of multiples is ⌊N/5⌋\lfloor N/5\rfloor for 1 through NN, not necessarily exactly N/5N/5.
  • Forgetting an endpoint. The inclusive interval from aa to bb contains b−a+1b-a+1 integers, not b−ab-a.
  • Counting only numbers ending in 5. Numbers ending in 0 are also divisible by 5.
  • Using the range formula for a digit-construction problem. If numbers are formed from restricted digits, count valid choices for each position instead of counting a consecutive interval.
  • Ignoring zero or negative numbers when they are included. Zero and every integer multiple of 5 are divisible by 5.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.