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.

  1. What happens with common multiples of 3 and 5?
  2. 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:

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.

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

Related Post

Jamie is a .NET developer specialising in ASP.NET MVC websites and services, with a background in WinForms and Games Development. When not programming using .NET, he is either learning about .NET Core (and usually building something cross platform with it), speaking Japanese to anyone who'll listen, learning about languages, writing for this blog, or writing for a blog about Retro Gaming (which he runs with his brother)