A place to hone our problem solving skills and enjoy eldritch art — and hopefully retain our sanity whilst doing so. I post a coding challenge every few days for us to solve together so subscribe at the very bottom of the page to receive email notifications :)
Ahead of me; an object that gleamed whitely in the newly bestowed rays of the ascending moon. That it was merely a gigantic piece of stone, I soon assured myself; but I was conscious of a distinct impression that its contour and position were not altogether the work of Nature. A closer scrutiny filled me with sensations I cannot express; for despite its enormous magnitude, and its position in an abyss which had yawned at the bottom of the sea since the world was young, I perceived beyond a doubt that the strange object was a well-shaped monolith whose massive bulk had known the workmanship and perhaps the worship of living and thinking creatures.
And we are live!
Without further ado lets take a look at our first problem which comes from Project Euler:
The problem: Link to problem
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
Before reading on, open the terminal and try solving this problem on your own first. The answer to this problem is 233168.
Since we are interested in multiples of 3 and 5 we definitely want to make use of the mod (%) operator. Those of you new to programming may not be familiar with the mod operator so let me just give you a brief introduction on how it works:
Ex 1: 10 ÷ 4 = 2, remainder 2 10 % 4 = 2 Ex 2: 16 ÷ 3 = 5, remainder 1 16 % 3 = 1 Ex 3: 20 ÷ 10 = 2, remainder 0 20 % 10 = 0
You guys see what's going on? For the first example the mod operator is basically saying 10 divided by 4, but return the remainder. 10 divided by 4 gives us a remainder of 2, hence 10 mod 4 is 2! In the third example, 10 is a multiple of 20, meaning there will be a remainer of 0 if we do 20 divided by 10, hence 20 % 10 is 0.
Now what does this mean in the context of the problem? It means that if we a number (n) is a multiple of 3, we'd expect n % 3 to give us 0. So onto our first order of business. How do we find all multiples of 3 and 5 in Python? Here's how:
Now here is where things get interesting because of how many ways we can go about finding the sum of all these numbers. Here's three:
Now out of all these methods, the first would definitely be my go-to for a multiple reasons.
Qualitatively, how would one know that method 1 is the quickest? I'll let you guys sort that out; but if you don't believe me here is some quantitative data I obtained using Python's timeit module.
And that's that! Hope you guys enjoyed this. I know this problem seems pretty elementary for some, but I promise that as time goes on we'll be dealing with more challenging problems. If you plan to stick around and want to receive email notifications whenever I post a new problem, subscribe with your email at the very bottom! You can always unsubscribe :)
Edit: Thank you to DrBones and philippachigalla for catching a mistake! My method 1 had a typo in it :)
Log in or Sign up to vote and leave comments.