EDIT (18/01/2013)
I have edited this post on the above date. However, the only change that I have made is that I am now hosting the source code (found at the end of this post) on GitHub. This means, that if I need to make a change to the code, I do not have to edit this post again – as it will pull the latest version of it from the relevant page on GitHub whenever it is accessed by a reader.
Recently, I joined Project Euler. It’s a great website full of programming puzzles.
This is going to be a post about coding, specifically in C++. But feel free to read along and join in even if you aren’t a code monkey, like me.
What Kind of Puzzles?
They’re maths based puzzles; which is great for code monkey’s like myself. Most coders seem to enjoy puzzles – it’s kind of what we do for a living – and mathematics related ones are even more tasty for me, since I love maths puzzles that I can code and code puzzles that I can quantify.
An Example
They’re first puzzle – which is really easy – goes a little something like this:
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.
That’s it. Simple, eh?
Worked Example
The real puzzle is the second line of the quote I gave, up there:
Find the sum of all the multiples of 3 or 5 below 1000.
I’ve made it even easier to figure it out by emboldening the important word there. Go ahead and have a go at drafting a solution. I’ll be waiting right here. …… …… ….
Oh, You’re Back
Here’s what I did: Firstly, I noticed that this puzzle is VERY SIMILAR to a puzzle called “FizzBuzz”; a puzzle that I solved in seconds, earlier this year. So, I took the code I’d written for that and altered it slightly. Here’s the code for FizzBuzz: Looking at this code, you should be able to see that it is very close to what we need.
Programmers do this from time to time. It’s called “code re-usage” and it’s the mark of a good programmer to be able to re-use and re-purpose code from seemingly unrelated projects.
There are only a few things that need changing to make it work.
- What happens with common multiples of 3 and 5?
- Do we need cout’s?
Multiple Multiples
The savvy amongst you would have realised that 3 and 5 have common multiples, namely 15, 20 and 45 (among others, that is). How do we stop our program from counting both of them? Quite easily, actually. We change the if..else branch contents of the for loop (lines 47 to 54) to the following:
1 2 3 4 |
if (i % 3 == 0) { //a multiple of 3 } else if (i % 5 == 0){ //a multiple of 5 that is not a multiple of 3 } |
Cout’s
We don’t need all those calls to cout in the code
Feature creep is the bane of all programmers in the world, so let’s not add features that we don’t need in this project
… so let’s remove them and replace them with some code to add the value of the current multiple to some variable. We’ll call it “sum” for now.
1 2 3 4 5 6 |
if (i % 3 == 0) { sum += i; } else if (i % 5 == 0){ sum += i; } |
The only thing we need to do now is add a variable called “sum” of type int at the beginning of the method; and a cout telling us what the answer is and we’ll be done. Here’s the finished code:
And all of that took me about 30 seconds to plan out in my head.
A minute or so to type, though.
It’s simple little puzzles like the above that improve your coding skills. Even if you start from a blank canvas.
Until next time,
J