some tips for project euler problems
- 2 minutes read - 219 wordsI wanted to make a list of a few tips on solving Project Euler problems that have been helpful for me while I solve them. These are general principles, even though I do most of my Project Euler coding in Python.
Without further ado:
- If the problem is asking for something concerning the number of digits, typically this indicates that the use of the $\log n$ function is warranted.
- If the problem is asking for the last few digits, modulo arithmetic might speed it up considerably.
- Some might consider this cheating, but looking up some small numbers in the Online Encyclopedia of Integer Sequences is occasionally pretty helpful.
- Many problems boil down to: Find numbers with property $X$ and property $Y$. Two solutions are:
- Brute force: Try all numbers with tests of property $X$ and $Y$.
- Find numbers with property $X$ and filter by a test of property $Y$.
- Find numbers with property $Y$ and filter by a test of property $X$.
- Find the set of numbers with property $X$ and the set of numbers with property $Y$. Compute their intersection.
I’ve found that it’s sometimes hard to predict which one will end up being the fastest. It depends on the relative speed of the tests and the generators, and the frequency of finding numbers which have that property.