Solving mathematical problems with Ruby

A couple of weeks ago I’ve discovered a very interesting and addictive website . They have a section called Project Euler where participants can try to resolve more than 100 mathematical problems. The scoring system is really well done and the discussion forum for each problem is accessible only after having solved the given problem.

It’s interesting to see how other people try to figure out a solution and what languages they employ to solve the puzzling problems. I even discovered rare languages like J or K through this site. It’s like a martial arts contest where participants can use their favorite martial art, only this is for hackers. 🙂

I’ve only spent a few hours over a couple of days on the site, enough time to solve 26 problems and bring me somewhere around 370th place out of more than 1300 active participants in the contest. I didn’t spend time solving problems during the last ten days, and in fact I am now 418th. I am going to work on more problems when I’ll have some time. I love this kind of stuff, and it’s not just for the enjoyable competition. It doesn’t really matter if you are first or last, especially if you know how to solve most of the problems but you are short of time like me. What’s important is to have fun and benefit from the learning process.

The programs must solve the given problems within 60 seconds (think of it like Gone in 60 Seconds), which means that most brute force methods won’t work.

This is particularly true for Ruby. In fact I have seen some people with C or Assembler resolve a problem with brute force algorithms, where Ruby would take way longer than a minute. There is no harm in this though, it’s a good exercise in controlling the algorithms’ complexity, facing problems that you don’t encounter too often in real world programming.

I use Ruby 1.8.4 so I even had to rewrite my own mathn library because prime computation and gcd are exceptionally slow (1.9 solves the problem though).

I invite you to join me and bring up Ruby statistics on MathsChallenge. And above all, have lots of fun…

Get more stuff like this

Subscribe to my mailing list to receive similar updates about programming.

Thank you for subscribing. Please check your email to confirm your subscription.

Something went wrong.