Project Nayuki

Project Euler solutions

Nayuki’s stats on Project Euler


I solve Project Euler problems to practice and extend my math and program­ming skills, all while having fun at the same time. Here I make my solutions publicly available for other enthusiasts to learn from and to critique. This page lists all of my Project Euler solution code, along with other helpful information like bench­mark timings and my overall thoughts on the nature of math and programming in Project Euler.

Each problem that I solved always includes a Java program. Almost all my solved problems also include a Python program (except for a few). Many problems additionally have a Mathematica and Haskell program. Numerous solutions contain a detailed mathematical proof to justify why the imple­mented algorithm is correct. Among the web, this is perhaps the largest collection of Project Euler solutions in the Java programming language. 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.


Solution code

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 my shared math library module:
Many Haskell solutions depend on my shared math library module: EulerLib.hs.
Some solution code contains a detailed mathematical proof of correctness.

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 Yes
Problem 19 p019.mathematica p019.hs Yes
Problem 20 p020.mathematica p020.hs Yes
Problem 21 p021.mathematica p021.hs Yes
Problem 22 p022.mathematica p022.hs Yes
Problem 23 p023.mathematica p023.hs
Problem 24 p024.mathematica p024.hs Yes
Problem 25 p025.mathematica p025.hs Yes
Problem 26 p026.mathematica p026.hs
Problem 27 p027.mathematica
Problem 28 p028.mathematica p028.hs Yes
Problem 29 p029.mathematica p029.hs Yes
Problem 30 p030.mathematica p030.hs Yes
Problem 31 p031.mathematica p031.hs Yes
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 Yes
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 p065.hs
Problem 66 p066.mathematica Yes
Problem 67 p067.mathematica p067.hs Yes
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 p085.hs
Problem 86
Problem 87
Problem 88 Yes
Problem 89 p089.mathematica p089.hs
Problem 90
Problem 91 p091.mathematica p091.hs
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 Yes
Problem 108 p108.mathematica Yes
Problem 109 p109.mathematica
Problem 111 Yes
Problem 112 p112.mathematica p112.hs
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 p128.hs Yes
Problem 129 p129.mathematica p129.hs
Problem 130 p130.hs
Problem 132 p132.mathematica p132.hs
Problem 133 p133.mathematica Yes
Problem 134 p134.mathematica Yes
Problem 135 Yes
Problem 139 p139.hs
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 p166.hs 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 178 p178.mathematica
Problem 179 p179.mathematica
Problem 182 p182.hs
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 p205.hs
Problem 206 Yes
Problem 208 Yes
Problem 211
Problem 214
Problem 215
Problem 216 Yes
Problem 218 p218.mathematica p218.hs Yes
Problem 222
Problem 225 p225.hs
Problem 231 p231.mathematica
Problem 243 p243.mathematica Yes
Problem 249
Problem 250 p250.mathematica
Problem 265 p265.hs
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 329 Yes
Problem 345
Problem 346 Yes
Problem 347
Problem 348 Yes
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 Yes
Problem 493
Problem 500 Yes
Problem 518 Yes
Problem 549 Yes
Problem 587

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 program­ming languages: Python and Mathematica are slow for basic integer arithmetic (~1/30× of Java speed) because they natively use bigint and are also dynami­cally 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. Also I study the numerical bounds carefully to avoid integer overflow, and use the most reasonably narrow type for speed (choosing between int, long, or BigInteger).

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



Almost all 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 familiar­ity 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 machine-word-sized integer values, many solutions are not worth the time to run it compared to the Java implementations.

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 impor­tant 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 program­ming 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’m a beginner at Haskell programming, and only know how to use it to solve the easier problems in Project Euler. When I do use it, I end up learning many new concepts in functional program­ming, such map/filter/fold, infinite lists, converting iterative algorithms to recursive functions, memoization, etc. When a problem can be solved in a purely functional way without impera­tively updating variables, my Haskell solution will be structured very similarly to my Mathematica solution.

To run a Haskell solution, run the Haskell file as a main program.

Sample code (problem 21):

main = putStrLn (show ans)
ans = sum [n | n <- [1..10^4], amicable n]

amicable :: Int -> Bool
amicable n = let m = divisorSum n in (m /= n) && (divisorSum m) == n

divisorSum :: Int -> Int
divisorSum n = sum [k | k <- [1..n-1], (mod n k) == 0]


Benchmark timings

The Project Euler solution programs listed above were bench­marked to see how much time it took to compute the answer. This information gives a rough sense of which problems are easy or hard, and how the choice of program­ming 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 “accept­able” 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 comprehen­sions) 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 computing environments were used:

Problem Java Python 2 Python 3 Mathematica Haskell
Problem 10.0000.0000.0000.0000.007
Problem 20.0000.0000.0000.0000.005
Problem 30.0030.0360.0010.0000.009
Problem 40.1230.2580.2942.1220.280
Problem 50.0050.0000.0000.0000.001
Problem 60.0000.0000.0000.0000.001
Problem 70.0340.1590.3200.0002.653
Problem 80.0020.0060.0030.0000.002
Problem 90.0080.0210.0361.4040.007
Problem 100.0550.2880.2991.2017.082
Problem 110.0020.0010.0020.0150.002
Problem 120.1682.3463.4710.0940.568
Problem 130.0050.0000.0000.0000.002
Problem 140.2637.6532.77171.6205.820
Problem 150.0010.0000.0000.0000.002
Problem 160.0020.0010.0000.0000.001
Problem 170.0030.0010.0010.0160.002
Problem 180.0000.0000.0000.0780.004
Problem 190.0010.0000.0000.0940.002
Problem 200.0010.0000.0000.0000.002
Problem 210.2390.0090.0120.1250.897
Problem 220.0350.0060.0090.0470.011
Problem 230.0771.7892.57612.48011.333
Problem 240.0350.0190.0190.6240.719
Problem 250.0110.0570.0580.2340.002
Problem 260.0460.0130.0150.0471.243
Problem 270.1061.2631.55821.325
Problem 280.0000.0000.0000.0000.001
Problem 290.0570.0120.0060.0160.409
Problem 300.0503.3453.44811.2480.566
Problem 310.0000.0000.0000.0310.475
Problem 320.2160.1170.13312.9480.046
Problem 330.0010.0000.0010.0310.002
Problem 340.1662.3203.38976.4412.486
Problem 350.1561.5371.32228.533
Problem 360.1150.3670.3663.5250.275
Problem 370.0721.0841.8303.495
Problem 380.0200.0270.0220.6240.011
Problem 390.1163.3935.56492.9920.063
Problem 400.0630.1850.2620.9980.164
Problem 410.0601.3092.2887.676
Problem 420.0060.0030.0040.0310.002
Problem 430.1454.2004.161147.187
Problem 440.78813.81313.111754.109
Problem 450.0340.0610.0760.421
Problem 460.0040.0240.0491.061
Problem 470.1021.4612.1380.718
Problem 480.0500.0020.0020.0000.005
Problem 490.0870.5650.62722.589
Problem 500.0470.3320.39914.274
Problem 510.1425.4277.112
Problem 520.0650.3510.2810.718
Problem 530.0530.0350.0110.0160.017
Problem 540.0330.0500.056
Problem 550.0800.0610.0460.4680.046
Problem 560.0820.3530.1700.0620.126
Problem 570.0500.0020.0022.137
Problem 580.1732.2743.9470.250
Problem 590.1634.5275.887
Problem 605.16599.407109.433347.520
Problem 610.0080.0030.003
Problem 620.0430.0950.062
Problem 630.0010.0010.0000.0000.002
Problem 640.5162.1264.7888.330
Problem 650.0010.0000.0000.0000.001
Problem 660.0780.1820.2350.811
Problem 670.0010.0020.0020.0310.007
Problem 680.0763.3363.923
Problem 690.0545.3953.6838.236
Problem 700.4419.99310.899181.787
Problem 710.0240.2630.2062.9950.187
Problem 720.0500.7280.7354.524
Problem 730.0734.2034.863
Problem 741.16821.85322.309
Problem 750.0740.4280.581
Problem 760.0020.0010.0020.0000.004
Problem 770.0010.0020.0030.0160.005
Problem 783.4162.4423.624
Problem 790.6835.4664.399
Problem 800.0940.0500.0270.047
Problem 810.0020.0030.0030.0460.009
Problem 820.0050.0060.0087.472
Problem 830.3900.0810.0961.092
Problem 842.63023.09127.095
Problem 850.0361.1941.01510.9200.442
Problem 860.67636.33051.903
Problem 870.2440.3640.335
Problem 881.43435.08929.112
Problem 890.0080.0120.0110.1870.003
Problem 900.0300.4520.465
Problem 910.0501.6967.37336.6600.059
Problem 920.61122.55522.936128.1704.139
Problem 930.84522.27612.483
Problem 942.29637.60982.190
Problem 950.6025.4095.005
Problem 960.1781.5111.547
Problem 970.0010.0000.0000.0000.059
Problem 980.40130.60455.627
Problem 990.0580.1430.1280.016
Problem 1000.0010.0000.000
Problem 1010.0030.0030.0020.000
Problem 1020.0010.0010.0010.0150.002
Problem 1040.7523.5683.51819.172
Problem 1050.0380.0730.072
Problem 1070.0030.0030.003
Problem 1080.1091.5653.0163.588
Problem 1090.0070.0120.0090.094
Problem 1110.0470.3630.235
Problem 1120.0671.9861.67313.6030.589
Problem 1130.0000.0000.0000.0000.002
Problem 1140.0000.0000.0000.0000.001
Problem 1150.0020.0000.0000.0160.002
Problem 1160.0000.0000.0000.0000.001
Problem 1170.0000.0000.0000.0000.001
Problem 1180.75025.80637.896
Problem 1190.0230.0340.018
Problem 1200.0000.0000.0000.0000.001
Problem 1210.0000.0000.0000.5150.014
Problem 1220.64259.21362.114
Problem 1230.0150.1430.1380.0310.016
Problem 1240.0830.1520.103
Problem 1250.0690.1270.148
Problem 1271.8932.9103.374
Problem 1280.0812.1581.6830.4371.537
Problem 1290.0590.2790.4425.4600.056
Problem 1300.1080.9401.4100.460
Problem 1320.0530.3310.5840.0470.555
Problem 1330.0770.0700.0570.140
Problem 1340.0500.4620.5095.335
Problem 1350.0510.9671.557
Problem 1390.0863.0484.0261.280
Problem 1420.21512.45523.113
Problem 1450.0000.0000.0000.0000.001
Problem 1469.746342.295238.903
Problem 1490.2289.78810.390
Problem 1500.40755.98769.623
Problem 1510.0100.0010.000
Problem 1557.90128.63753.360
Problem 1600.0460.8340.5107.5200.383
Problem 1620.0010.0000.0000.0000.002
Problem 1640.0200.0550.0500.1560.047
Problem 1660.4208.82913.8731.149
Problem 1690.0240.0050.0040.031
Problem 1710.0660.4490.4731.809
Problem 1720.0020.0000.0010.000
Problem 1730.0200.3050.35410.4830.102
Problem 1740.0390.4370.4849.797
Problem 1780.0180.0160.0120.047
Problem 1790.97014.66517.58183.601
Problem 1820.7951.3665.37811.242
Problem 1860.45511.1998.327
Problem 1870.3218.5568.7911354.640
Problem 1880.0010.0020.0030.00013.634
Problem 1910.0000.0000.0000.0000.001
Problem 1970.0110.0000.0010.032
Problem 2030.1122.2872.1320.015
Problem 2040.1107.2916.293
Problem 2050.0010.0000.0000.0000.001
Problem 2060.962212.057149.398
Problem 2082.65215.81810.554
Problem 2112.788377.014202.205
Problem 2141.48651.05335.261
Problem 2150.7521.0871.190
Problem 2166.53485.97565.468
Problem 2180.0000.0000.0000.0000.001
Problem 2226.204202.054239.576
Problem 2250.1183.3153.8640.332
Problem 2311.89728.37225.138141.836
Problem 2430.0000.0000.0000.000
Problem 2491.451124.80188.255
Problem 2500.27218.73213.192302.969
Problem 2650.760251.220175.081235.198
Problem 2670.8242.3392.619
Problem 2710.0100.0080.003
Problem 2800.474
Problem 3010.0000.0000.0000.0000.001
Problem 3030.51332.82027.767
Problem 3043.1128.6157.102
Problem 3150.1509.0909.017
Problem 3230.0010.0000.0001.0760.001
Problem 3450.0260.1590.186
Problem 3460.2671.5210.763
Problem 3470.3292.0861.922
Problem 3480.83835.66034.996
Problem 3571.55156.13394.017
Problem 3810.77413.86316.679122.508
Problem 3871.63215.24810.516
Problem 4016.46047.53732.299499.921
Problem 4074.02675.38879.901
Problem 41713.946
Problem 4251.69552.92659.466
Problem 4290.75521.20618.23394.068
Problem 431721.048
Problem 433> 1 day
Problem 45128.159696.983490.033
Problem 4930.0110.0060.003
Problem 5001.5716.8071.929
Problem 5184.75880.792190.622
Problem 5492.46987.123100.481
Problem 5870.0060.0040.004

More info