Project Euler solutions
I did Project Euler problems for fun to practice my math and programming skills. I have publicly made available the code that I wrote to solve the problems. My code is written in a mix of Java, Mathematica, and Haskell. For each problem, I used whichever language was the most convenient to solve the problem. I wrote all the code myself and did not base it on anyone else’s code.
Solutions
Numerical answers to all problems I solved: Answers.txt
All Java solutions depend on a class with shared algorithms: Library.java
Source code
My Project Euler solution code is available at GitHub: https://github.com/nayuki/Project-Euler-solutions
You can browse individual files on GitHub, or download a ZIP of everything.
Java
The majority of my solutions are implemented in Java, my preferred general-purpose programming language. I use this language to implement algorithms from scratch (especially ones that heavily use imperative state updates) to take advantage of the speed and compactness of fixed-width integers, or to precisely control which values are memoized. However, my Java solutions tend to be quite long, so I prefer to use Mathematica when possible.
To run a Java solution, compile the Java file (e.g. p001.java) and also the shared class Library.java. Then run with a command like java p001.
Sample code (problem 117) (most other solutions are a few times longer):
public class p117 {
public static void main(String[] args) {
long[] ways = new long[51]; // Memoization
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i <= 50; i++) {
for (int j = 1; j <= 4 && j <= i; j++)
ways[i] += ways[i - j]; // Dynamic programming
}
System.out.println(ways[50]);
}
}
Resources:
- Official site: Oracle Java
Mathematica
I used Mathematica for many of the earlier problems, because compactness/convenience was more important than time or space performance. I also used it when I needed easy access to prime numbers, big integers, big floats, fractions, continued fractions, and such. My code tends to be quite short: one-liners are very common, and typically the solution is less than 5 lines of code. For problems that involve computing and/or storing millions of numbers, my experience has been that Mathematica takes too long to run the code or uses too much memory.
In Mathematica, I make heavy use of nested expressions, functional programming functions like Map, Select, and aggregation functions like Total, Length, Max. Occasionally I write imperative code in Mathematica, usually for unbounded searching.
To run a Mathematica solution, copy all the code to a new Mathematica notebook and evaluate the entire block. The solution is given by the output.
Sample code (problem 7):
Prime[10001]
Sample code (problem 20):
Total[IntegerDigits[100!]]
Sample code (problem 29):
Length[Union[Flatten[Table[Table[a^b, {b, 2, 100}], {a, 2, 100}]]]]
Sample code (problem 323):
cdf[n_] := (1 - (1/2)^n) ^ 32
pdf[n_] := cdf[n] - cdf[n - 1]
Sum[n * pdf[n], {n, Infinity}] (* Exact *)
N[%, 11] (* Rounded *)
Resources:
- Official site: Wolfram Mathematica
- Function reference: Wolfram Mathematica Documentation
Haskell
I rarely use Haskell to solve Project Euler problems. When I do use Haskell, I find it to be ideal for defining simple functions that are unrelated to number theory.
To run a Haskell solution, run the Haskell file as a main program.
Sample code (problem 22):
import List (sort) import Char (ord) names = ["MARY", ... (omitted) ... "ALONSO"] strSum = sum . (map (\c -> (ord c) - (ord 'A') + 1)) ans = sum (zipWith (*) [1..] (map strSum (sort names))) main = putStrLn (show ans)
Resources:
- Official site: The Haskell Programming Language
- Tutorial: Learn You a Haskell for Great Good!