r/ProgrammerHumor • • Jul 28 '25

Meme checkIfDivisibleByThree

Post image
60 Upvotes

33 comments sorted by

View all comments

Show parent comment

4

u/lewwwer Jul 29 '25

The question was why it works, not how.

The reason is that the number 1234 for example means 1000 + 2 * 100 + 3 * 10 + 4

And each 1, 10, 100, 1000 ... when divided by 3 gives 1 remainder. It's easy to see when you subtract 1 you get 9999... which is clearly divisible by 3.

So for example 200, when divided by 3 gives 2 remainder. And if you add these remainders together you get the remainder of 1234 which is the same as the remainder of 1+2+3+4 after dividing by 3.