Divisibility Rules

For this challenge we will investigate a range of algorithms used to apply some of the most common divisibility rules. We will first focus on the divisibility rule of 3 to introduce the concept of recursion.

A divisibility rule is a shorthand way of determining whether a given integer is divisible by a fixed divisor without performing the division, usually by examining its digits

Divisibility rule

So, for this challenge we will focus on implementing the divisibility rules using some python scripts without using the DIV (//) or MOD (%) operators. In reality, these operators would be a more elegant way of checking if a number is divisible by another number.

Divisibility Rule for 2

A number is divisible by 2 if its last digit is a multiple of 2. (e.g. 0,2,4,6,8)

Divisibility rule for 2

Let’s look at the following example: is 378 divisible by 2? Yes because its last digit, 8 is a multiple of 2.

Let’s write a Python a function to implement this divisibility rule for 2:

Divisibility Rule for 3

A number is divisible by 3 if the sum of all its digits is divisible by 3.

Divisibility rule for 3

Let’s look at the following example: is 378 divisible by 3?
divisibility-rule-for-3

Our aim is to write a function that takes a number as a parameter and returns whether this number is divisible by 3. To implement this function we will use a recursive function (A function with a call to itself).

Your Task


Your task is to adapt the above algorithms to implement the divisibility rules for 4,5,6.

A number is divisible by 4, if the number formed by its last two digits is divisible by 4.

Divisibility rule for 4

A number is divisible by 5, if its last digit is either a 0 or a 5.

Divisibility rule for 5

A number is divisible by 6, if it is divisible by 3 and by 2.

Divisibility rule for 6

Extension Task: Divisibility rule for 7


Apply a recursive method to find out is a number is divisible by 7. First you will need to read more about the divisibility rule for 7 on this wikipedia page.

Share Button
Tagged with: