Sean Barton - Game Developer

Fun programming exercises with Project Euler

23 December 2009

Hi there! This is an old post that may reference technology or views I now consider outdated - or maybe even a little embarrassing. Enjoy at your own risk.

Leonhard Euler

I don’t know about programmers in the real world but as a video game programmer I often worry about my coding skills getting a little bit stagnant due to our near-universal reliance on C++. And that problem is even worse if you work at one of those places where fancy stuff like templates, the STL, and smart pointers are discouraged (because “they’re slow”, or something) and especially so if inheritance gone wild is encouraged.

Yes, again with that Pragmatic Programmer book

People that know me are well aware of my love for The Pragmatic Programmer. I don’t enjoy going around telling people what to do, nor do I pretend to have the authority to do so, but I think it’s one of those books that should be read by anyone who enthusiastically writes code for a living.

A very popular bit of advice from that book is to learn a new programming language every year. I actually only manage to learn a new language every couple of years, but it’s still valuable advice. Learning C#, for instance, was one of the better moves for me as I’ve found more and more game studios are using C# for their tools development.

Lately, I’ve been looking for a good scripting language to add to my repertoire. And by good I mean not Python or Perl (tongue firmly implanted in cheek, btw). I actually have extensive experience with Python (most of Tabula Rasa was written in Python) but I can’t stand the whitespace syntax. And Perl? Well, let’s just say that when it comes to a programming language that I am a very shallow man – and a good personality just isn’t enough.

Autobots and Decepticons fight

So I’ve decided to give this Ruby thing a try, for no other reason than because everyone seems to be talking about it. Besides, it was invented in Japan. I mean, come on, this is the nation that gave us Sushi, The Transformers, and Nintendo – so you know Ruby has to be awesome. ‘Nuff said. Case closed. Q.E.D.

But learning a new language can be kind of boring

I find the hardest part of teaching yourself a new programming language is that you need practice – and those first few exercises, on the order of Hello World, just aren’t that exciting. It’s kind of like sitting down with a piano teacher and going over the C major scale. Screw that, I want to play Für Elise already. But I guess you have to go through that boring stuff before you can accomplish something meaningful.

Project Euler provides cool exercises for programmers

Through Math-Blog, I have recently discovered a totally cool site for mathematicians and programmers, Project Euler:

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

And here’s the money quote from Math-Blog’s Antonio Cangiano explaining how this relates to us folks in search for exciting ways to self-teach a new computer language:

Finally, if you are a programmer who’s scoping out a new programming language, be it Python, Ruby, Scala, Haskell or Erlang, you’ll find a great ally in Project Euler. Having to write hundreds of programs in a given language, will naturally increase your familiarity with that language. And again, comparing your newcomer coding style with those of more experience participants, will no doubt contribute to your advancement within the given language you’re focusing on.

Right on. For my own part, I just finished my first solution to Problem #9:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Project Euler frowns upon just giving out answers on the internet, but I don’t mind saying that a quick look at Wikipedia for Pythagorean triple generating functions helped me get to a solution that wasn’t entirely brute force, and I learned just a little bit more Ruby in doing so.

Pythagorean Theorem

+1 Math and +1 Ruby? Nice.

And what I found really awesome was, after entering the answer key on Project Euler’s website, I was granted access to a special forum for that solution, and was rather, um, humbled by the much more elegant solutions that existed, both in terms of mathematical knowledge and mastery of a given language. This is very cool stuff for learning.

269 bottles of math on the wall

There are currently 269 problems up in Project Euler to solve but I doubt I’ll ever do more than a dozen or so over time. I have a ton of other projects on the go and I just want an interesting problem or two to work on as I grasp a new programming language, though if I’m really tempted, I just might do enough to become a Level 1 Eulerian. 🙂

As an enthusiast of both programming and mathematics, Project Euler has my highest recommendation. Check them out.

Seanba

Hi! My name is Sean Barton (aka Seanba).

I’m an American-Canadian software developer working in Austin, Texas where I never have to shovel snow. :fire:

My career focuses exclusively on video game software and tools. You can read more about me here.


Aseprite2Unity

Aseprite2Unity imports sprites and animations for use with your Unity projects. Like all my tools it is available for free or name your price.

Get Aseprite2Unity.


SuperTiled2Unity

SuperTiled2Unity is a collection of Unity scripts that import Tiled Map Editor files to your Unity projects. You can download for free or name your price.

Get SuperTiled2Unity.

(Note that regular Tiled2Unity is now deprecated.) :zzz:


Seanba

Like my tools? Consider making a donation as a way of thanks!