Project Nayuki

Project Euler solutions

Nayuki’s stats on Project Euler


This page lists all of my Project Euler solution code, along with other helpful information like benchmark timings.

Each problem that I solved includes a Java program. Many problems also include a Python, Mathematica, and/or Haskell program as well. Some solutions include a detailed mathematical proof to justify why the implemented algorithm is correct. Among the web, this is perhaps the largest collection of Project Euler solutions in the Java programming language.

I do Project Euler 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, although a couple of times I obtained the high-level approach from reading various literature on the web.



Git repositories

Web host Browse files Download package Numerical answers
GitHub (primary) Browse files Download ZIP Answers.txt
Eigenstate (mirror) Browse files Download .tar.gz Answers.txt

Individual files

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

Language usage


For every problem that I solved, I have a Java solution for it (and possibly code in other languages as well). I like using Java because it is fast, safe, and expressive. To elaborate on these points, I will compare it to other programming languages: Python and Mathematica are slow for basic integer arithmetic (~1/30× of Java speed) because they natively use bigint and are also dynamically typed. Also, Mathematica uses a lot of memory to store an array of integers because it doesn’t have packed fixed-width integers. C and C++ are unsafe because of the lack of array bounds checking, having signed integer overflow, and having tons of easily invoked undefined behaviors. Custom data structures like graphs are difficult to express cleanly in Mathematica. Custom algorithms like the sieve of Eratosthenes, especially ones most naturally expressed in terms of imperative state updates, are difficult to implement correctly or efficiently in Haskell. Non-strict evaluation in Haskell makes it easy to accidentally leak large amounts of memory in unexpected places.

However on the flip side, I prefer to solve a problem in Mathematica first if it’s possible. This is because it has many useful built-in mathematical functions (like prime testing) and objects (like fractions) that would require manual effort to implement in Java. Also, my Java solutions tend to be long due to types on every variable, lots of custom code, and low-level loops instead of higher-order functions. If I was aiming for raw execution speed, writing comparable code in C or C++ would probably run 3× as fast as Java.

Note that for problems involving non-whole numbers, I try to use exact integer arithmetic or fractions as much as possible, which ensures that the solution is provably correct. As a result I strongly avoid any floating-point arithmetic at all, unless there is no other reasonable way (that I know of) to solve the problem.

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, and the answer will be printed to standard output.

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];



Many solutions are made available in Python, which is an imperative object-oriented language with many conceptual similarities to Java. These pieces of code are interesting for a couple of reasons: One is that it caters to programmers who prefer to read Python code over Java due to familiarity with the language. Another is that the Python code has less syntactic “noise” due to the lack of types, variable declarations, and integer size distinctions – so the Python code expresses the essential ideas of the mathematical algorithms more directly. My Python code is designed to be compatible with Python 2.7 and 3.4 alike.

The Python solutions were initially based on the Java solutions, often starting with a direct literal port of the Java code into Python. Over time, the Python code was adapted to fit the characteristics of the language – such as using idiomatic/Pythonic approaches, tweaking or changing algorithms to increase speed (whereas Java can sometimes get away with less efficient but simpler algorithms), and making heavy use of generators. The style of using generators/filters/itertools can be considered about halfway between Java’s imperative style and {Haskell or Mathematica}’s functional style. Unfortunately due to Python’s slow performance on arrays and short integer values, it will be difficult for me to make Python solutions available for the CPU- or memory-intensive Project Euler problems.

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 exceeds the memory limit.

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. I write Mathematica code in a rather plain style, using only [] for function application (not @ or //), avoid pattern processing, and avoid declaring functions with the #-and-& syntax.

To run a Mathematica solution, copy the entire code into a new Mathematica notebook and evaluate the whole block of text. 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)


Benchmark timings

The Project Euler solution programs listed above were benchmarked to see how many seconds it took to compute the answer. This information gives a rough sense of which problems are easy or hard, and how the choice of programming language affects the running time.

Note that the benchmark does not attempt to be “fair” in any way. My solution code is first designed to run within an “acceptable” running time (not targeting absolute fastest code), and then heavily optimized for human clarity (both in terms of the code implementation and the underlying mathematical concepts). Sometimes, slightly inefficient constructs (such as list comprehensions) are used for the sake of clarity. The algorithms between different languages are not exactly the same; instead I try to write code that is most idiomatic and clear for the given language. However, it can still be observed that Python is generally 10× to 30× slower than Java for pure number-crunching code.

All the numbers listed in the table below are in seconds, and these programming environments were used:

Problem Java Python 2 Python 3 Mathematica
Problem 10.0003230.0000.0010.000
Problem 20.0001980.0000.0000.016
Problem 30.0142010.0440.0010.000
Problem 40.1077500.2500.3053.328
Problem 50.0011230.0000.0000.000
Problem 60.0001940.0000.0000.000
Problem 70.0336200.1580.3790.000
Problem 80.0023860.0060.0020.000
Problem 90.0082240.0200.0352.265
Problem 100.0479170.2810.2901.797
Problem 110.0014520.0020.0020.032
Problem 120.1581212.3004.9050.140
Problem 130.0044330.0000.0000.000
Problem 140.2799321.1421.321114.187
Problem 150.0005890.0000.0010.000
Problem 160.0019380.0000.0000.000
Problem 170.0025110.0000.0010.031
Problem 180.0002470.0000.0000.141
Problem 190.0013030.0000.0010.031
Problem 200.0010000.0000.0000.000
Problem 210.2468810.0090.0120.203
Problem 220.0336980.0060.0080.109
Problem 230.0789311.7632.53019.766
Problem 240.0332960.0220.01821.891
Problem 250.0103270.0280.0280.406
Problem 260.0515870.0140.0200.078
Problem 270.1132280.6821.05634.063
Problem 280.0002430.0000.0000.000
Problem 290.0424190.0120.0070.031
Problem 300.0458023.2733.21618.109
Problem 310.0004880.0000.0000.047
Problem 320.2129670.1170.16420.890
Problem 330.0007530.0010.0000.062
Problem 340.1621542.2854.006125.062
Problem 350.1625241.3921.35248.328
Problem 360.1092110.3610.3915.610
Problem 370.0826401.0722.0495.953
Problem 380.0194970.0270.0260.453
Problem 390.1107483.3325.581151.453
Problem 400.0656130.1830.3151.813
Problem 410.0446011.2942.34213.125
Problem 420.0060500.0040.0040.047
Problem 430.1535194.1504.569250.453
Problem 440.78214413.66714.636
Problem 450.0074140.0590.0760.672
Problem 460.0037190.0230.0521.797
Problem 470.0922311.4582.4931.156
Problem 480.0542270.0020.0020.031
Problem 490.0605890.5760.63140.594
Problem 500.0409080.3280.39419.719
Problem 510.1461805.3127.807
Problem 520.0684200.3620.3181.156
Problem 530.0467550.0350.0110.016
Problem 540.0491460.0540.072
Problem 550.0872450.0590.0470.750
Problem 560.0895070.3400.1640.094
Problem 570.0580300.0030.0033.156
Problem 580.1772552.2535.430
Problem 590.1761804.4396.947
Problem 605.097925
Problem 610.0079590.0070.008
Problem 620.0349820.0980.067
Problem 630.0005880.0000.0000.000
Problem 640.4977072.0755.80212.390
Problem 650.0006950.0000.0000.000
Problem 660.0971750.1780.2881.204
Problem 670.0014080.0010.0020.047
Problem 680.0827333.2534.452
Problem 690.0621470.6950.977
Problem 700.4269699.95212.126
Problem 710.0242820.2580.2544.750
Problem 720.0539110.7000.8338.140
Problem 730.0678964.1045.088
Problem 741.13374821.46930.048
Problem 750.1032740.4360.699
Problem 760.0022060.0010.0020.000
Problem 770.0005090.0020.003
Problem 783.4165842.3694.129
Problem 790.6900745.3625.059
Problem 800.0863130.0490.0240.078
Problem 810.0020020.0030.0040.078
Problem 820.0045660.0070.00810.703
Problem 830.2960380.0790.1091.922
Problem 842.646967
Problem 850.0341111.1811.091
Problem 860.606965
Problem 870.2375610.3850.342
Problem 881.41768934.53436.862
Problem 890.0081390.0110.0110.250
Problem 900.0313770.3410.378
Problem 910.0405991.5992.274
Problem 920.68149421.05823.931209.875
Problem 930.857063
Problem 942.28169036.95698.736
Problem 950.585060
Problem 960.1889051.4891.833
Problem 970.0010160.0000.0000.016
Problem 980.385149
Problem 990.0656660.1410.177
Problem 1000.0007390.0010.001
Problem 1010.0030020.0040.0110.000
Problem 1020.0011720.0010.002
Problem 1040.6825783.5223.44629.312
Problem 1050.0380460.0750.085
Problem 1070.003129
Problem 1080.1030721.5453.559
Problem 1090.0064550.0080.013
Problem 1110.042826
Problem 1120.0663332.0141.95722.938
Problem 1130.0002770.0000.0000.000
Problem 1140.0002950.0000.0000.000
Problem 1150.0019490.0000.0000.000
Problem 1160.0002100.0000.0010.000
Problem 1170.0001980.0000.0000.000
Problem 1180.72931325.30946.411
Problem 1190.0228360.0340.018
Problem 1200.0002590.0010.0000.000
Problem 1210.0003530.0000.0000.875
Problem 1220.64355157.05069.450
Problem 1230.0150470.1430.1330.046
Problem 1240.1062620.1890.100
Problem 1250.0629780.1290.153
Problem 1272.191093243.007388.778
Problem 1280.0763832.1412.039
Problem 1290.0542540.2790.5279.000
Problem 1300.1070790.9261.606
Problem 1320.0596160.3280.6580.094
Problem 1330.0747380.0690.0580.219
Problem 1340.0464630.4510.5509.391
Problem 1350.0480140.9621.646
Problem 1420.20755512.24823.281
Problem 1450.0001870.0010.000
Problem 1469.786555
Problem 1490.2397069.41510.498
Problem 1500.39962454.84466.795
Problem 1510.0077430.0000.000
Problem 1557.60310027.97763.322
Problem 1600.0460270.7780.70712.235
Problem 1620.0005900.0000.0000.000
Problem 1640.0198380.0550.054
Problem 1660.3995478.65713.616
Problem 1690.0243310.0030.003
Problem 1710.0636450.4410.4822.954
Problem 1720.0018540.0010.0010.000
Problem 1730.0204760.3050.34615.953
Problem 1740.0379690.4270.479
Problem 1790.96603914.46518.471
Problem 1820.7961571.3476.713
Problem 1860.48807811.0179.395
Problem 1870.3152318.4068.359
Problem 1880.0014840.0020.0030.000
Problem 1910.0003210.0000.0000.016
Problem 1970.0071410.0010.0010.062
Problem 2030.1104422.2412.063
Problem 2040.1049557.1636.890
Problem 2050.0007380.0000.000
Problem 2060.957665
Problem 2112.807356
Problem 2141.46409250.34437.674
Problem 2166.57209985.06565.802
Problem 2180.0001680.0000.0000.000
Problem 2226.409021
Problem 2250.1218732.2222.218
Problem 2311.87831227.98229.958
Problem 2430.0003040.0000.0000.000
Problem 2491.432286128.61889.561
Problem 2500.27599718.49714.199
Problem 2656.448999
Problem 2670.8178682.2973.114
Problem 2710.0103740.0040.003
Problem 2800.507408
Problem 3010.0001900.0000.0000.000
Problem 3030.53346236.70329.034
Problem 3043.084105
Problem 3150.1605618.3849.444
Problem 3230.0005460.0000.0001.641
Problem 3450.0259240.1510.196
Problem 3571.503228419.912101.565
Problem 3810.80174113.37416.248205.875
Problem 3871.62735615.03610.071
Problem 4016.50482940.79230.460850.921
Problem 4074.005450
Problem 41713.983912
Problem 4251.715204
Problem 4290.75562620.40916.693155.015
Problem 431716.572513
Problem 433> 1 day
Problem 45128.201323

More info