Learning To Code

Join me as I take the plunge!

Top 3 Reasons to Try Project Euler

A few days before I started the Flatiron School, Dave introduced me to Project Euler.

This is a website with hundreds of math problems that you need to solve by building a comptuer program. It is named after the 18th century Swiss mathemetician Leonhard Euler, who according to wikipedia is “one of the most prolific mathematicians ever”.

While there is some disagreement over how to pronounce Euler (some say ‘oil-er’, some say ‘you-ler’), there is a broad consensus that this is a great way to learn how to become a better programmer. However many people are intimidated by this website, since it involves math.

I’ve been going through these problems over the last few weeks and have a git repository of my work. I can say that yes it is a bit challenging, but that you learn so much that you should stop worrying, and just start doing the problems. I’ll take the problem I finished last night, and use it to show what I’ve been able to learn.

1
2
3
4
Summation of primes
Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

1. You Get A Math Refresher

Instead of being intimidated by hard math concepts, just view it as an opportunity to hone your math skills! Math was always one of my favotite subjects in school because it made sense, and if I worked hard enough I could find a definitive answer. Perhaps I’m a bit unique in this, but doing math problems is really fun for me. For this problem, I had to jog my memory about prime numbers. I remembered that prime numbers were numbers greater than 1 that had no other factors besides 1 and itself. Not anything enlightening, but good to know I still remember middle school math concepts.

2. You Are Forced To Break Down Problems

I then realized I needed to break this problem down into many smaller problems. This has been one of the most critical skills I’ve developed while at the Flatiron School. If you are unable to take a big problem and break it down into smaller sub-problems, you won’t be able to do much of anything in programming. Project Euler has been one of the best resources I’ve found to practice doign this.

Here is how I broke down problem 10.

1
2
1. find all prime numbers under 2,000,000
2. take all these numbers and add them together

Once I started the first step I realized I could break it down further.

1
2
3
a. Find all the numbers under 2,000,000
b. Check if each one is prime
c. If it is prime, add it into an array

For the second step, I already knew that what I was doing (taking an array and adding it together) perfectly fit the pattern of reduce, and so didn’t need to further break it down. Avi should be proud that I’ve developed this kind of intuition.

3. You Learn How to Refactor

I ended up with some code that worked (the answer is 142,913,828,922), but was extremely slow

1
2
3
4
5
6
7
8
9
10
require 'prime'

array = []
(1...2000000).each do |num|
  if num.prime?
    array << num
  end
end

puts array.reduce{ |sum, n| sum + n }

At this point, I let myself go on the internet and try to find a way to refactor. I stumbled upon the ruby prime guide and realized that the prime module had more functionality that I could use to speed up my program. Here is my new code that I made after doing that research:

1
2
3
4
5
6
array = []
Prime.each(2000000) do |prime|
  array << prime
end

puts array.reduce{ |sum, n| sum + n }

It turns out that Prime is a class that is basically an array of every prime number. Since I am now only iterating over prime numbers as opposed to all numbers, my program went considerably faster - running in about half a minute as opposed to the several minutes it took to run the original.

Refactoring is a concept that some people get intimidated by, but by starting small and taking baby steps you can build up confidence to take on more challenging problems like refactoring huge controllers in rails.

So to all those on the fence about trying Project Euler - I say GO DO IT!