## Project Euler tips

A few years back I started working on Project Euler problems mainly because it was fun from a mathematical point of view, but also to improve my programming skills. After solving about 120 problems I seem to have hit a wall, because the numbers involved in some of the problems were just too big for my simple brute-force algorithms.

Recently, I decided to try and see if I can do some more of these problems. I cannot say that I’ve acquired some new techniques between 2012-2016 concerning the mathematics involved in these problems. My research topics are usually quite different and my day to day programming routines are more about constructing new stuff which works fast enough than optimizing actual code. Nevertheless, I have some experience coding in Matlab, and I realized that nested loops are to be avoided. Vectorizing the code can speed up things 100 fold.

So the point of Project Euler tasks is making things go well for large numbers. Normally all problems are tested and should run within a minute on a regular machine. This brings us to choosing the right algorithms, the right simplifications and finding the complexity of the algorithms involved.

For example if you need to compute a certain sum what is the best way to proceed? Computing each in turn and summing them up in a loop gives us an algorithm. This means that computation time is roughly proportional to (this depends also on , but let’s suppose is something which we can compute in time, that is, instantly). Therefore, if is of order , this can be done on a regular machine. However, if , it may take, literally, ages for the algorithm to finish. For an algorithm of complexity , which scales with the square root of , should work. However let’s say you have in mind. Then either we find some algorithm with large enough or, even better, find a algorithm, which scales like . Roughly, if the algorithm works for , it should only take times more time to reach . Best of all, if you can find a direct formula for the sum, then you have an “instant” algorithm (whether this is instant or not depends on the formula you got…).

Maybe this could become more clear using some examples.

1. . The basic summation is time, but using the formula gives an algorithm. Basically, you can compute this sum for any you want by doing a couple of elementary operations.

2. Let’s complicate things a bit. What if is : the sum of all the divisors of ? First let’s note that computing alone is not immediately computable. One can imagine a basic algorithm, testing all integers if and summing them. If we know the factorization of we have a formula for in terms of prime factors and their powers. Prime factorization’s complexity is provided we have already made a sieve of primes (that’s other thing to think about). So we reach a complexity for . This is not enough if we want to sum things up to , for example. It turns out there’s a simple idea for a less complex algorithm.

We want to compute

Now what if we invert the sums? How do we do this? The summation indices satisfy . Thus, if we want to sum over instead of , then would be a multiple of all the time. So we find the alternative sum

where the last equality is valid since the number of multiples of less than is exactly . This already gives an complexity. Better than before, no factorizations involved, quite nice.

It turns out we can do even better. Looking at terms of the form we see that they take the same value multiple times. For example implies

Thus, summing on this interval we have

where the last sum has an explicit value since we sum consecutive terms of an arithmetic progression. Now we can switch to a summation over instead of . Note that we only need to go to high enough such that between and there is at least one integer. This is equivalent to so is . For larger than this value, we cannot guarantee the existence of an integer in our interval, so we’ll just compute the sums up to this and then sum the remaining terms which are again of order . This gives us a final algorithm.

Now, it is clear that if we need to compute our sum of sums of divisors up to , for example, only an algorithm like the last one can lead us to the result in reasonable time. Now you can solve Problem 401 in no time 🙂 (replacing the sum of divisors with sum of squares doesn’t make much difference here, or does it?)

I hope this will motivate others to gain or regain interest in Project Euler problems. I solved over 120 problems in the last two months by learning to adapt algorithms and doing the necessary research when it is needed. Almost always, after solving a problem, you’ll find someone in the solution thread which has a better solution than yours. Look it up and continue improving your skills.

Project Euler problems don’t require only maths… Sometime more complex computer science stuff is needed. I learned the basics of dynamic programming and recursion solving some of the problems. I learned using lists, queues, combinatorial stuff, probabilities (still learning that though; lots of probability related stuff among PE problems). Not to mention that I learned to work quite well in some different languages, like Pari GP or Julia. It also forced me to compile and use some Java or Python codes.

It is a globally enriching quest: keep up with your maths, learn to do research online, improve programming skills, find new algorithms, choose right programming language to solve your problem efficiently, etc. It’s a hobby worth having.