Honing Your Programming Skills with Project Euler

There's nothing quite like that feeling you get when you finally figure out the solution to a problem. Whether it is solving a complex math equation, beating your friend at Tic-Tac-Toe, or even successfully programming the DVR for the first time. As programmers, we are constantly looking for new problems to solve and interesting ways to improve our programming skills.

Project Euler is a programming challenge site that hosts (currently) 272 programming problems of varying difficulty. Users can log in, attempt to solve each problem, and discuss each solution.

Here is an example of one of the Project Euler problems.

Problem #1

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.

The Solution

This problem instantly reminds me of the FizzBuzz problem, so I think I'll start with that for my solution.

One of my goals for this year is to become more proficient in Ruby and C#, so I'll solve each problem using a mixture of both languages.

The Ruby Solution

max = 1000
sum = 0

(1..(max-1)).each do |i|
	 if ((i % 3 == 0) || (i % 5 == 0))
		sum += i
	 end
end

puts sum

The C# Solution

int max = 1000;
int sum = 0;

for (int x = 1; x < max; x++) {
	if ((x % 3 == 0) || (x % 5 == 0)) {
		sum += x;
	}
}

Console.WriteLine(sum);

Pretty straightforward, right? Simply sum up all of the values that are evenly divisible by 3 or 5 and print the final sum. This is probably the solution 99% of programmers would come up with on their first attempt. But, what about other ways of solving the same problem? For instance, can this problem be solved without looping through each number?

A better way to solve the problem

One of the best parts about Project Euler is the large community of smart developers all working on the same problem sets. Once you submit a correct answer to a problem, you will gain access to a forum thread for that particular problem and will be allowed to discuss your solution with everyone else who has solved the same problem.

This is one of the more interesting solutions I saw on the problem (converted to Ruby)

x = 1000;
puts 1.5*((x-1)/3)*((x+2)/3) + 2.5*((x-1)/5)*((x+4)/5) - 7.5*((x-1)/15)*((x+14)/15); 

This solutions utalizes arithmetic progression to create a more elegant solution to the original problem.

Would you think of solving this problem using arithmetic progression? Probably not (at least for the first attempt anyway), I sure didn't.

Conclusion

Project Euler is a great way to improve your programming and problem solving skills. I hope this brief introduction was interesting enough to motivate you to check Project Euler out for yourself and try to solve some other problems on your own. Practicing these types of problems will (at the very least) better prepare you for your next job interview, which is never a bad thing.

Image From: Mathematician Pictures

Creative Commons License

What do you think?