Algorithms, Blockchain and Cloud

Teaching Kids Programming – Proof of Rule: Integer Divisible By 3


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given a integer, we want to find out if it is divisible by 3. We can sum up the digits and check if the sum is divisble by three.

For example, 12345, the digits sum is 1+2+3+4+5 which is 15 and 15 can be divisble by 3 and thus original integer 12345 is divisble by 3.

Proof of Rule: Integer Divisible By 3

Let thus
We can subtract 1 multiple from each component and add them separately, which is:


The aka in the 9999… is clearly a multiple of 3’s. And thus if n can be divisble by 3, the digits sum (a+b+c+d+e) needs to be divisble by three as well.

def divisbleBy3(n: str):
    s = 0
    for i in n:
        s += int(i)
    return s % 3 == 0

And one-liner:

def divisbleBy3(n: str):
    return sum(int(i) for i in list(n)) % 3 == 0

–EOF (The Ultimate Computing & Technology Blog) —

411 words
Last Post: Teaching Kids Programming - Topological Sort Algorithm on Directed Graphs (Course Schedule, BFS)
Next Post: Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm

The Permanent URL is: Teaching Kids Programming – Proof of Rule: Integer Divisible By 3 (AMP Version)

Exit mobile version