Project Nayuki

Project Euler solutions

Nayuki’s stats on Project Euler

This page hosts a collection of my solutions to Project Euler problems.

All the problems I solved include a Java program as a solution. Some include a Python, Mathematica, and/or Haskell program as well. Among the web, this is perhaps the largest collection of Project Euler solutions in the Java programming language.

I solve Project Euler problems for fun to practice and extend my math and programming skills. Here I make my solutions publicly available for other enthusiasts to learn from and to critique. All my code is original work, not based on anyone else’s code, though sometimes I obtained the high-level approach from reading various literature on the web.


All my files in this project are available in a Git repository:

Web site Browse files Download package
GitHub (primary) Browse files Download ZIP
Eigenstate (mirror) Browse files Download .tar.gz

Numerical answers to all problems I solved: Answers.txt

Some solution source code includes detailed comments to explain and justify the mathematics involved in the algorithm. The solutions without explanations generally use straightforward techniques and are self-explanatory from the code.

Every Java solution depends on these shared classes:,
Many Python solutions depend on the shared module:

Problem Java Python Mathematica Haskell Explanation
Problem 1 p001.mat.txt p001.hs
Problem 2 p002.mat.txt p002.hs
Problem 3 p003.mat.txt p003.hs Yes
Problem 4 p004.mat.txt p004.hs
Problem 5 p005.mat.txt p005.hs
Problem 6 p006.mat.txt p006.hs Yes
Problem 7 p007.mat.txt p007.hs
Problem 8 p008.mat.txt p008.hs
Problem 9 p009.mat.txt p009.hs
Problem 10 p010.mat.txt p010.hs
Problem 11 p011.mat.txt p011.hs
Problem 12 p012.mat.txt p012.hs
Problem 13 p013.mat.txt p013.hs
Problem 14 p014.mat.txt p014.hs
Problem 15 p015.mat.txt p015.hs
Problem 16 p016.mat.txt p016.hs
Problem 17 p017.mat.txt p017.hs
Problem 18 p018.mat.txt p018.hs
Problem 19 p019.mat.txt p019.hs
Problem 20 p020.mat.txt p020.hs
Problem 21 p021.mat.txt p021.hs
Problem 22 p022.mat.txt p022.hs
Problem 23 p023.mat.txt p023.hs
Problem 24 p024.mat.txt p024.hs
Problem 25 p025.mat.txt p025.hs
Problem 26 p026.mat.txt p026.hs
Problem 27 p027.mat.txt
Problem 28 p028.mat.txt p028.hs Yes
Problem 29 p029.mat.txt p029.hs
Problem 30 p030.mat.txt p030.hs Yes
Problem 31 p031.mat.txt p031.hs
Problem 32 p032.mat.txt Yes
Problem 33 p033.mat.txt Yes
Problem 34 p034.mat.txt p034.hs Yes
Problem 35 p035.mat.txt
Problem 36 p036.mat.txt p036.hs
Problem 37 p037.mat.txt
Problem 38 p038.mat.txt p038.hs
Problem 39 p039.mat.txt p039.hs
Problem 40 p040.mat.txt p040.hs
Problem 41 p041.mat.txt
Problem 42 p042.mat.txt p042.hs Yes
Problem 43 p043.mat.txt
Problem 44 Yes
Problem 45 p045.mat.txt
Problem 46 p046.mat.txt
Problem 47 p047.mat.txt
Problem 48 p048.mat.txt p048.hs
Problem 49 p049.mat.txt
Problem 50 p050.mat.txt
Problem 51
Problem 52 p052.mat.txt
Problem 53 p053.mat.txt p053.hs
Problem 54
Problem 55 p055.mat.txt p055.hs
Problem 56 p056.mat.txt p056.hs
Problem 57 p057.mat.txt
Problem 58 p058.mat.txt
Problem 59
Problem 60 p060.hs Yes
Problem 61
Problem 62
Problem 63 p063.mat.txt p063.hs Yes
Problem 64 p064.mat.txt
Problem 65 p065.mat.txt
Problem 66 p066.mat.txt
Problem 67 p067.mat.txt p067.hs
Problem 68
Problem 69 p069.mat.txt
Problem 70 p070.mat.txt
Problem 71 p071.mat.txt p071.hs
Problem 72 p072.mat.txt
Problem 73 Yes
Problem 74
Problem 75 Yes
Problem 76 p076.mat.txt p076.hs
Problem 77 p077.mat.txt p077.hs Yes
Problem 78 Yes
Problem 79
Problem 80 p080.mat.txt Yes
Problem 81 p081.mat.txt p081.hs
Problem 82 p082.mat.txt
Problem 83 p083.mat.txt
Problem 84
Problem 85 p085.mat.txt
Problem 86
Problem 87
Problem 88
Problem 89 p089.mat.txt p089.hs
Problem 90
Problem 91
Problem 92 p092.mat.txt p092.hs
Problem 93
Problem 94 Yes
Problem 95
Problem 96
Problem 97 p097.mat.txt p097.hs
Problem 98
Problem 99
Problem 100
Problem 101 p101.mat.txt Yes
Problem 102 p102.mat.txt p102.hs
Problem 104 p104.mat.txt Yes
Problem 105
Problem 107
Problem 108 Yes
Problem 109
Problem 111 Yes
Problem 112 p112.mat.txt
Problem 113 p113.mat.txt p113.hs Yes
Problem 114 p114.mat.txt p114.hs Yes
Problem 115 p115.mat.txt p115.hs Yes
Problem 116 p116.mat.txt p116.hs Yes
Problem 117 p117.mat.txt p117.hs Yes
Problem 118
Problem 119 Yes
Problem 120 p120.mat.txt p120.hs Yes
Problem 121 p121.mat.txt p121.hs Yes
Problem 122
Problem 123 p123.mat.txt p123.hs Yes
Problem 124
Problem 125
Problem 127
Problem 128 p128.mat.txt Yes
Problem 129 p129.mat.txt
Problem 130
Problem 132 p132.mat.txt
Problem 133 p133.mat.txt Yes
Problem 134 p134.mat.txt Yes
Problem 135 Yes
Problem 142 Yes
Problem 145 Yes
Problem 146 Yes
Problem 149
Problem 150
Problem 151
Problem 155
Problem 160 p160.mat.txt p160.hs Yes
Problem 162 p162.mat.txt p162.hs Yes
Problem 164 p164.mat.txt p164.hs Yes
Problem 166 Yes
Problem 169 p169.mat.txt
Problem 171 p171.mat.txt Yes
Problem 172 p172.mat.txt Yes
Problem 173 p173.mat.txt p173.hs Yes
Problem 174 p174.mat.txt
Problem 179 p179.mat.txt
Problem 182
Problem 186
Problem 187 p187.mat.txt Yes
Problem 188 p188.mat.txt p188.hs
Problem 191 p191.mat.txt p191.hs
Problem 197 p197.mat.txt Yes
Problem 203 p203.mat.txt
Problem 204
Problem 205 p205.mat.txt
Problem 206 Yes
Problem 211
Problem 214
Problem 218 p218.mat.txt p218.hs Yes
Problem 222
Problem 225
Problem 231 p231.mat.txt
Problem 243 p243.mat.txt
Problem 249
Problem 250 p250.mat.txt
Problem 265
Problem 267 Yes
Problem 271
Problem 280
Problem 301 p301.mat.txt p301.hs Yes
Problem 303
Problem 304
Problem 315
Problem 323 p323.mat.txt p323.hs Yes
Problem 345
Problem 357
Problem 381 p381.mat.txt Yes
Problem 387
Problem 401 p401.mat.txt Yes
Problem 407 Yes
Problem 417 Yes
Problem 425 Yes
Problem 429 p429.mat.txt Yes
Problem 431
Problem 433 Yes
Problem 451

Language usage


The majority of my solutions are first 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 first solve in Mathematica when possible.

To run a Java solution, compile the Java file (e.g. and also the shared classes and Then run with a command like java p001.

Sample code (problem 117) (most other solutions are many times longer):

public class p117 {
  private static final int LENGTH = 50;
  public static void main(String[] args) {
    // Dynamic programming
    long[] ways = new long[LENGTH + 1];
    ways[0] = 1;
    for (int n = 1; n <= LENGTH; n++) {
      for (int k = 1; k <= 4 && k <= n; k++)
        ways[n] += ways[n - k];
    return Long.toString(ways[LENGTH]);



Some solutions are made available in Python as a courtesy to programmers who are more familiar with this language and/or don’t read any of the other languages. Like Java, Python is an imperative and object-oriented programming language, so the solution approach will be largely the same, unlike functional languages.

The advantage of Python is that the code is more compact than Java, lacking type declarations, braces, and some parentheses; algorithms get distilled down to their essential ideas. The disadvantage is that Python performs poorly on large data – computing billions of arithmetic operations is slow, and storing millions of numbers in memory is takes much more space than using packed fixed-width integers. It will be difficult for me to make Python solutions available for the CPU- or memory-intensive Euler problems.

My Python code is designed to be compatible with Python 2.x and 3.x alike.

Sample code (problem 10):

# Sieve of Eratosthenes
ans = 0
isprime = [True] * 2000001
for i in range(2, len(isprime)):
	if isprime[i]:
		ans += i
		for j in range(i * i, len(isprime), i):
			isprime[j] = False



I used Mathematica for many of the earlier problems, because compactness and convenient library functions were more important than running time or memory. Mathematica provides easy access to prime numbers, big integers, high-precision floats, fractions, continued fractions, and more. 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 my algorithm or runs out of memory.

In Mathematica, I make heavy use of nested expressions, functional programming core functions like Map and Select, and aggregation functions like Total, Length, Sum. Occasionally I write imperative code in Mathematica, usually for unbounded searching.

To run a Mathematica solution, copy the entire code into a new Mathematica notebook and evaluate the whole block. The solution is shown in the output.

Sample code (problem 7):


Sample code (problem 20):


Sample code (problem 29):

Length[Union[Flatten[Table[a^b, {a, 2, 100}, {b, 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 *)



I sometimes use Haskell to solve Project Euler problems. When I do use it, I end up learning many new concepts in functional programming, such map/filter/fold, infinite lists, converting iterative algorithms to recursive functions, memoization, etc.

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)