Python Lessons Learned From Project Euler

January 2, 2008

Much of my winter vacation has been spent playing around with the problems on Project Euler. I love crosswords and other puzzles, and I’ve posted before about how I love the MacHeist game that’s currently going on. But Project Euler is different from all the puzzles I’ve solved before.

The idea is to write short little programs to solve math problems that Euler himself was interested in (and, since Euler was interested in a huge variety of problems, there’s little risk of running out of topics). For example, Problem 1 is:

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.
This is typical of most of the PE problems: they describe a problem, give a solution to an easy version of the problem, and then pose the hard version of the problem. The easy version is useful for working out stupid bugs in ones code. Python is ideal for solving these problems (and, indeed, is the 2nd most popular language for solving Project Euler problems, after C/C++). The arbitrary length integers make even large integer problems fairly easy to tackle, and the language has good support for string manipulations, which are also quite useful.One of the qualities that sets PE apart from other puzzles is that I feel that I’m enriching my math and programming skills by solving the problems. Indeed, I find that I’m learning or reinforcing some fairly important lessons. Here’s what I’ve learned in solving the first 70 problems, in no particular order:

  • Use sets or dicts for membership testing; the sets in particular are amazingly versatile: they’re not much harder to use than a normal list, but you immediately get uniqueness, and you can always convert back by taking list(set);
  • range() is fine for small lists, but takes up a large amount of memory for billion-element lists. xrange() is trivial to use in its place;
  • It’s always good to store your intermediate results in a list such that list.sort() returns your final answer without sifting through pages of data;
  • Always solve a small version of the problem before tackling a large problem; make the solution automatic enough that by changing a single number, you can go from the easy version to the hard version.
Advertisements

3 Responses to “Python Lessons Learned From Project Euler”


  1. […] already mentioned how much I love Project Euler, and it’s gratifying to see Python making such a respectable […]

  2. gelatin Says:

    project euler was actually assigned in my python class as a bonus to make up for our quizzes.


  3. The preice is $47 USD wityh no shipping costs because this courtse is 100% downloaded from the internet.It’s pretty
    easy to copy just abot any blues lick especially if you have a video and diagrams
    to follow but your goal should be to expand on licks to make them your own. Some book on music marketing, promotion, and how to run a business online and offline are easily available in thee
    market.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: