Programming the first 50 Euler Problems, here is what I learned

Pierre Blanchet
5 min readApr 2, 2021

--

Practice makes perfect, programming is no exception

Photo by Artur Shamsutdinov on Unsplash

If you are interested in programming you might already know the Project Euler. It’s a great website that basically challenges you with problems, from very easy to very hard. The problems are usually math related but don’t worry, you don’t need to be a math genius to understand the problems or solve them. What you need is to know a bit of coding and the willingness to try.

Which programming language you use doesn’t matter, the website will just ask you to give the answer to the problem to check if you solved it.

I discovered this website thanks to one of my roommates and I have to admit I’ve been a bit addicted to it recently. It’s actually very satisfying to work on a problem where you know a solution exists. Even if it’s sometimes very hard to find.

Develop your programming skills

Solving these problems is a great way to test and improve your programming skills. You will have to find smart solutions to complicated problem. Also, you cannot always brute force you way to the solution. Sometimes it just cannot work, it would take to long to run on even the best computer out there.

I personally use python to solve these problems. It’s easy to learn, read, and the online documentation and existing libraries are just so good.

You can find my code here to the different Euler problems. But I wouldn’t recommend checking the code before trying the problem yourself. In my opinion, what’s more important to share is what I learned from doing these problems.

1) Think a little bit before jumping into it

Most of these problems can be solved really quickly if you have the right idea. But sometimes this idea doesn’t appear instantly in your mind. You might want to jump into the code and try to program something quickly. But that’s a bad idea.

Just take 10–15 minutes to read carefully the exercise. Try to see if there is a pattern to look for to find the answer. Taking this time might save you hours down the road.

You cannot imagine the time I lost on some problems, rushing into the first solution I thought of. Then realizing that it won’t work and finding a better solution that takes 5 minutes to code and a few milliseconds to process.

2) People are actually amazing

What’s great with Project Euler is that once you have solved a problem, you have access to the thread related to it. And inside it people usually put their code and explanation on how they solved the challenge.

Learn from it. It’s crazy how good some people are at math and/or programming. Sometimes they have solutions in one line of code that takes 10 ms to process while yours is 20 lines and struggles to give the result in less than a minute.

It might feel a bit overwhelming to see how quick and efficient other people are. But you shouldn’t see it that way. It’s more like having free programming teachers that give you an insight into how to do great code.

Also, don’t worry too much about which programming language you use. Usually there is always someone who has a brilliant solution in it.

3) Processing time matters

Photo by Ryan on Unsplash

If you read the homepage of the website, they tell you that each problem can be solved by an algorithm in under one minute with a normal computer. From what I’m seeing, this is definitely true.

And by looking at other people’s code I realized that it’s possible to optimize running time. Here are what people usually do to fasten their code :

  • Use specialized library for specific calculations. Like numpy for vectors.
  • Use complex programming languages that are better for specific tasks. If you look at the threads, you might sometime see some programming languages that you have never heard about. You look at the code : it’s unreadable. But it runs 30 times faster than your own code.
  • Parallelize their operation when possible (using for example jit and numbs https://numba.pydata.org/)
  • Try different things. For example is a for loop faster than a while loop (Spoiler, in python the for loop is faster) ? Is it better to keep intermediate variable or calculate complicated expression with list comprehension ? Can you cache the values to fasten your calculation.
  • Reducing complexity. Sometimes if you really think about the problem you can realize that you don’t need to calculate all the options. That can dramatically reduce your processing time.

Reducing run time is a very good practice to understand better how coding works. Also, if you want to work as a programmer, any application is better off the faster it runs.

4) Math is hard

Before this website, I thought I was good at math. I’m an aeronautic engineer. I know physics. I can calculate complex aerodynamic equations with so many characters we need to use several alphabets to write it.

But this project really humbled me. Math is a vast subject with a lot of varied disciplines and subtleties. And truly, I know nothing. For a lot of problems, there are some tricky math properties that can be used to find an even better solution.

It was even humiliating sometimes. People found answers to some problems by hand ! Just casually noticing a property that only PhD’s are aware of. Taking a pen and paper and in 2 minutes, they have the answer. That’s truly amazing to witness and this is another reason why you should have a look at the thread of the problems.

5) Practice makes perfect

Deep inside us we all know it. The best way to learn is to do it ourselves. And in programming it cannot be more true. I’ve learned so much by just trying, failing and starting again from scratch. It’s sometimes a hard process, but a truly enjoyable one.

When you check your solution and you finally have the big green check telling you it’s right, you feel amazing.

So I would advise you to go right away to the website. Try the first problems. Code. You will learn a lot and become a truly great coder.

--

--

Pierre Blanchet

Entrepreneur / Content Creator. I am dedicating my life to fighting climate change and be inspiring while doing so.