Project Euler solutions

Nayuki's stats on Project Euler

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

Problem Mathematica Java Haskell
Problem 1 p001.mat.txt p001.java p001.hs
Problem 2 p002.mat.txt p002.java
Problem 3 p003.mat.txt
Problem 4 p004.mat.txt
Problem 5 p005.mat.txt
Problem 6 p006.mat.txt p006.hs
Problem 7 p007.mat.txt
Problem 8 p008.mat.txt
Problem 9 p009.mat.txt p009.hs
Problem 10 p010.mat.txt
Problem 11 p011.java
Problem 12 p012.mat.txt
Problem 13 p013.mat.txt
Problem 14 p014.java
Problem 15 p015.mat.txt
Problem 16 p016.mat.txt
Problem 17 p017.java
Problem 18 p018.java
Problem 19 p019.java p019.hs
Problem 20 p020.mat.txt
Problem 21 p021.mat.txt
Problem 22 p022.hs
Problem 23 p023.java
Problem 24 p024.java
Problem 25 p025.mat.txt
Problem 26 p026.java
Problem 27 p027.java
Problem 28 p028.java
Problem 29 p029.mat.txt p029.hs
Problem 30 p030.mat.txt
Problem 31 p031.mat.txt p031.java
Problem 32 p032.mat.txt
Problem 34 p034.mat.txt
Problem 35 p035.mat.txt
Problem 36 p036.mat.txt
Problem 37 p037.java
Problem 38 p038.hs
Problem 39 p039.java
Problem 40 p040.java
Problem 41 p041.java
Problem 42 p042.java
Problem 43 p043.java
Problem 44 p044.java
Problem 45 p045.mat.txt
Problem 46 p046.java
Problem 47 p047.mat.txt
Problem 48 p048.mat.txt p048.hs
Problem 49 p049.java
Problem 50 p050.java
Problem 51 p051.java
Problem 52 p052.mat.txt
Problem 53 p053.mat.txt
Problem 54 p054.java
Problem 55 p055.mat.txt
Problem 56 p056.mat.txt
Problem 57 p057.mat.txt
Problem 58 p058.java
Problem 59 p059.java
Problem 62 p062.java
Problem 63 p063.mat.txt
Problem 64 p064.mat.txt
Problem 65 p065.mat.txt
Problem 67 p067.java
Problem 69 p069.java
Problem 70 p070.java
Problem 71 p071.mat.txt
Problem 72 p072.java
Problem 73 p073.java
Problem 74 p074.java
Problem 75 p075.java
Problem 76 p076.mat.txt
Problem 77 p077.java
Problem 78 p078.java
Problem 79 p079.java
Problem 80 p080.mat.txt
Problem 81 p081.java
Problem 82 p082.java
Problem 83 p083.java
Problem 85 p085.java
Problem 89 p089.java
Problem 92 p092.mat.txt p092.java
Problem 95 p095.java
Problem 96 p096.java
Problem 97 p097.mat.txt
Problem 99 p099.java
Problem 102 p102.java
Problem 104 p104.mat.txt p104.java
Problem 107 p107.java
Problem 108 p108.java
Problem 114 p114.java
Problem 115 p115.java
Problem 116 p116.java
Problem 117 p117.java
Problem 122 p122.java
Problem 124 p124.java
Problem 125 p125.java
Problem 145 p145.java
Problem 160 p160.mat.txt p160.java p160.hs
Problem 179 p179.java
Problem 186 p186.java
Problem 187 p187.java
Problem 188 p188.java
Problem 191 p191.java
Problem 197 p197.mat.txt
Problem 204 p204.java
Problem 206 p206.java
Problem 211 p211.java
Problem 214 p214.java
Problem 250 p250.java
Problem 323 p323.mat.txt

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:

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:

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: