Kodeclik Logo

Our Programs

Learn More

Schedule

Kodeclik Blog

How do you test if a number is divisible by 3? In math we are taught that we must sum all the digits of the number and if the sum is divisible by 3, then the original number is also divisible by 3. For instance, given the number 27, the sum of its digits is 2+7=9. 9 is divisible by 3; therefore 27 is divisible by 3.

In Python we have many ways to check if a given number is divisible by 3. Let us count the ways!

The simplest and most straightforward method is using the modulo operation:

In this function we directly compute the remainder when the number is divided by 3, using the % operator, and if the remainder is zero, we return True, else False.

Another interesting method is based on implementing the divisibility rule for 3:

The above Python function first converts the absolute value of the input number n to a string using str(abs(n)), which allows it to handle both positive and negative integers. The function then iterates through each character (digit) in this string, converting each back to an integer, and calculates the sum of all these digits using a generator expression within the sum() function. This sum is stored in the variable digit_sum. Finally, the function checks if digit_sum is itself divisible by 3 using the modulo operator (%).

For very large numbers, we can use a recursive approach:

This method repeatedly sums the digits until we get a single-digit number, which is then checked for divisibility by 3. For single-digit numbers (n < 10), it directly checks if n is 0, 3, 6, or 9. For larger numbers, it calculates the sum of the digits of the absolute value of n and recursively calls itself with this sum. The function continues this process until it reaches a single-digit number, effectively implementing the digit sum method for divisibility by 3.

For a more efficient method with large numbers, we can use bitwise operations:

This method exploits properties of binary numbers divisible by 3. It exploits the property that for even numbers, divisibility by 3 is equivalent to divisibility of n/4 by 3, while for odd numbers, it's equivalent to divisibility of (n-1)/2 by 3. The function uses bitwise AND (&) and right shift (>>) operations to efficiently perform these checks without actual division.

Each of the above methods has its advantages depending on the specific use case and the size of the numbers being tested.

Multiples of 3

Here is a very simple program to find all multiples of 3 from 1 to 100:

This code defines the is_divisible_by_3 function, which uses the modulo operator to check if a number is divisible by 3. Then, it uses a list comprehension to create a list of all numbers from 1 to 100 that are divisible by 3. Finally, it prints the result.

The output of this code will be:

This list contains all the multiples of 3 from 1 to 100, as expected. The largest multiple of 3 less than or equal to 100 is 99, which is included in the list.

Of course it is very easy to update the above code to find all multiples of a different number, say 5, from a different range, say 1 to 1000.

This function, find_multiples, takes two arguments: x (the number to find multiples of) and y (the upper limit of the range to search). It uses a list comprehension to create a list of all numbers from 1 to y (inclusive) that are divisible by x. The modulo operator (%) is used to check divisibility, where i % x == 0 means i is divisible by x without a remainder. When called with arguments 3 and 100, it efficiently generates a list of all multiples of 3 from 1 to 100.

This function is versatile and can be used to find multiples of any number within any specified range, making it useful for various mathematical and programming tasks, like we have done above in the bottom portion of the program, i.e., finding all multiples of 5 from 1 to 100.

The output will be:

What if we needed to find all multiples of 3 or 5 in Python from 1 to 100? That should be simple, right? We just combine the above two lists! Well, not so fast. We need to make sure we do not double count numbers like 15, 30, etc.

Note that this is not the same as finding multiples of 15. We still would like to retain multiples like 6, 9, etc. We just dont want to double count multiples. How do we do that?

To find all multiples of 3 or 5 from 1 to 100 without double counting, we can update the code using the find_multiples() function as follows:

This solution elegantly solves the problem of finding multiples of 3 or 5 without double counting. The find_multiples() function takes a list of numbers and an upper limit y as arguments. It uses a set to store the multiples, which automatically eliminates duplicates. The function iterates through each number in the input list, adding its multiples to the set. Finally, it returns a sorted list of unique multiples. When we run this code with numbers = [3, 5] and y = 100, it produces a list of all multiples of 3 or 5 up to 100, including numbers like 6 and 9, while avoiding double counting of common multiples like 15 and 30.

The output will be:

In summary, we have seen four different ways of checking for divisibility by 3 and then used that function to analyze a large set of numbers. Have fun extending this program to satisfy other use cases!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.