Solutions to Project Euler problems in C++
https://en.wikipedia.org/wiki/Project_Euler
From Wikipedia:
Project Euler is a website dedicated to a series of computational problems intended to be solved with computer programs. The project attracts adults and students interested in mathematics and computer programming.
Project Euler asks users to not share their solutions beyond problem 100, and for those that do to try to make the content educational. To that end I am providing approach explanations and linking resources useful links in this README.
- These are my original solutions written in C++.
- The intent was to solve these problems as quickly as possible: I frequently time myself.
- Comments are sparse and the intended approach is to be written quickly.
- Solution values, timing on my laptop, and a terse solution approach are given below.
- I'm also adding a series of useful links to topics useful to each problem at the end.
- On occasion I will consider different approaches to the problem: the one I end of using remains in the main.cpp, while the discarded/incomplete solution will be in another file.
For most problems they can be compiled and run with the following commands:
g++ -std=c++0x main.cpp
./a.out
For problems requiring big integers or multi-precison arithmetic, the GNU Multiple Precision library is used:
g++ -std=c++0x main.cpp -lgmpxx -lgmp
./a.out
Name: Multiples of 3 and 5
Solution: 233168
Timing: 0.071106ms
Approach: Brute force. (closed-form is possible)
Name: Even Fibonacci numbers
Solution: 4613732
Timing: 0.056059ms
Approach: Iterative brute-force. (closed-form is possible)
Name: Largest prime factor
Solution: 6857
Timing: 0.152476ms
Approach: Sieve of Eratosthenes.
Name: Largest palindrome product
Solution: 906609
Timing: 0.395974ms
Approach: Search with largest-first ordering.
Name: Smallest multiple
Solution: 232792560
Timing: 0.075001ms
Approach: Closed-form solution via GCD prime factors.
Name: Sum square difference
Solution: 25164150
Timing: 0.058018ms
Approach: Closed-form solution via triangular number and square pyramidal numbers.
Name: 10001st prime
Solution: 104743
Timing: 40.0103ms
Approach: Sieve of Eratosthenes.
Name: Largest product in a series
Solution: 23514624000
Timing: 1.05348ms
Approach: Brute force search.
Name: Special Pythagorean triplet
Solution: 31875000
Timing: 0.283833ms
Approach: Brute force search.
Name: Summation of primes
Solution: 142913828922
Timing: 1288.29ms
Approach: Sieve of Eratosthenes
Name: Largest product in a grid
Solution:70600674
Timing: 1.92247ms
Approach: Brute force search
Name: Highly divisible triangular number
Solution: 76576500
Timing: 72.1384ms
Approach: Sieve of Eratosthenes, then cache maps of prime factors to powers, multiply through to get total divisors. Also maps to triangular numbers.
Name: Large sum
Solution: 5537376230
Timing: 1.11715ms
Approach: Long addition
Name: Longest Collatz sequence
Solution: 837799
Timing: 5454.24ms
Approach: Cache smaller chain length in a map
Name: Lattice paths
Solution: 137846528820
Timing: 0.070639ms
Approach: Closed-form solution via star-and-bars forumla. Just needed a few tricks to compute without int overflow.
Name: Power digit sum
Solution: 1366
Timing: 4.65102ms
Approach: Long addition
Name: Number letter counts
Solution: 21124
Timing: 0.137673ms
Approach: Base 10 digits and lookup tables.
Name: Maximum path sum I
Solution: 1074
Timing: 0.452704ms
Approach: Dijkstra's algorithm
Name: Counting Sundays
Solution: 171
Timing: 0.69525ms
Approach: Direct counting with look-up table. Closed-form may be possible with calendar calculations.
Name: Factorial digit sum
Solution: 648
Timing: 0.32855ms
Approach: Long addition.
Name: Amicable numbers
Solution: 31626
Timing: 2.83573ms
Approach: Sieve of Eratosthenes for primes, cache divisor sums, then use geometric series property to quickly compute new abundancy indicies.
Name: Names scores
Solution: 871198282
Timing: 13.1327ms
Approach: Reading strings, ascii math, direct.
Name: Non-abundant sums
Solution: 4179871
Timing: 1380.09ms
Approach: See Problem 21
Name: Lexicographic permutations
Solution: 2783915460
Timing: 0.077083ms
Approach: Close to closed-form with factorial number system.
Name: 1000-digit Fibonacci number
Solution: 4782
Timing: 0.090347ms
Approach: Close-form solution via Binet's formula
Name: Reciprocal cycles
Solution: 983
Timing: 75.9181ms
Approach: Long division to find remainder cycle.
Name: Quadratic primes
Solution: -59231
Timing: 92.1775ms
Approach: Sieve of Eratosthenes.
Name: Number spiral diagonals
Solution: 669171001
Timing: 0.056059ms
Approach: Closed-form solution via triangular number and square pyramidal numbers.
Name: Distinct powers
Solution: 9183
Timing: 7.61125ms
Approach: Use of a set and a dictionary to keep track.
Name: Digit fifth powers
Solution: 443839
Timing: 56.6929ms
Approach: Estimate bounds, then generate multi-choose.
Name: Coin sums
Solution: 73682
Timing: 0.104569ms
Approach: Classic dynamic programming with sub-problem look-up table.
Name: Pandigital products
Solution: 45228
Timing: 2282.32ms
Approach: Generate all permutations and search operator placement.
Name: Digit cancelling fractions
Solution: 100
Timing: 0.220548ms
Approach: Brute-force, then Euclid's for lowest terms solution.
Name: Digit factorials
Solution: 40730
Timing: 3690.61ms
Approach: Compute upper bound, then brute-force (could be optimized by lowering upper-bound).
Name: Circular primes
Solution: 55
Timing: 637.492ms
Approach: Sieve of Eratosthenes
Name: Double-base palindromes
Solution: 872187
Timing: 3591.2ms
Approach: Radix conversion.
Name: Truncatable primes
Solution: 748317
Timing: 20.0482ms
Approach: Sieve of Eratosthenes, tree search.
Name: Pandigital multiples
Solution: 932718654
Timing: 2432.7ms
Approach: Generate permutations, and check grouping validity
Name: Integer right triangles
Solution: 840
Timing: 1.99543ms
Approach: Search all valid right-angle triangles, keep count of perimeters with a map.
Name: Champernowne's constant
Solution: 210
Timing: 0.055696ms
Approach: Direct computation (closed-form possible).
Name: Pandigital prime
Solution: 7652413
Timing: 2194.3ms
Approach: Permutation generation and prime testing.
Name: Coded triangle numbers
Solution: 162
Timing: 2.81879ms
Approach: Quadratic equation to check if triangular.
Name: Sub-string divisibility
Solution: 16695334890
Timing: 261.677ms
Approach: Generate permutations, check progressively.
Name: Pentagon numbers
Solution: 5482660
Timing: 77.0005ms
Approach: Brute force (using correct search order)
Name: Triangular, pentagonal, and hexagonal
Solution: 1533776805
Timing: 1.76118ms
Approach: Brute force (using correct search order)
Name: Goldbach's other conjecture
Solution: 5777
Timing: 3.32399ms
Approach: Sieve of Eratosthenes
Name: Distinct primes factors
Solution: 134043
Timing: 129.825ms
Approach: Sieve of Eratosthenes
Name: Self powers
Solution: 9110846700
Timing: 79.5088ms
Approach: Big integer addition and arithmetic, and some modulo properties.
Name: Prime permutations
Solution: 296962999629
Timing: 5.20908ms
Approach: Sieve of Eratosthenes.
Name: Consecutive prime sum
Solution: 997651
Timing: 1.11369ms
Approach: Sieve of Eratosthenes
Name: Prime digit replacements
Solution: 121313
Timing: 2180.88ms
Approach: Sieve of Eratosthenes for primes, generate number masks by iterating bitmasks via binary. Map families.
Name: Permuted multiples
Solution: 142857
Timing: 579.317ms
Approach: Brute force. (Though there seems to be a relation to the digital expansion of 1/7)
Name: Combinatoric selections
Solution: 4075
Timing: 0.140582ms
Approach: Pascale's rule.
Name: Poker hands
Solution: 376
Timing: 7.5457ms
Approach: Straighforward file and data processing.
Name: Lychrel numbers
Solution: 249
Timing: 164.759ms
Approach: Long addition, brute force.
Name: Powerful digit sum
Solution: 972
Timing: 761.328ms
Approach: Long multiplications and exponentiation by squaring.
Name: Square root convergents
Solution: 153
Timing: 69.4136ms
Approach: Big integer addition.
Name: Spiral primes
Solution: 26241
Timing: 390.926ms
Approach: A little algebra, plus sieve of Eratosthenes to check primality.
Name: XOR decryption
Solution: 129448
Timing: 589.954ms
Approach: Brute force the key. Check decrypted text for containing the top 10 most common English words (as per description).
Name: Prime pair sets
Solution: 26033
Timing: 4465.47ms
Approach: Join prime-pairs as an edge in a graph. The problem then maps to the clique problem.
Name: Cyclical figurate numbers
Solution: 28684
Timing: 1.51747ms
Approach: Compute all figurates needed in range. Compute cycles of the number and join in a graph if there is a join. Find a path that touches all.
Name: Cubic permutations
Solution: 127035954683
Timing: 89.1774ms
Approach: Generate cubes, keep track of perms by using digit counts.
Name: Powerful digit counts
Solution: 49
Timing: 0.076835ms
Approach: Use logarithm laws and inequalities to find bounds and search through easily.
Name: Odd period square roots
Solution: 1322
Timing: 511.809ms
Approach: A little algebra allows direct approach.
Name: Convergents of e
Solution: 272
Timing: 1.0735ms
Approach: Big integer algebra.
Name: Diophantine equation
Solution: 661
Timing: 273.055ms
Approach: This is actually Pell's equation, whose solutions can be found via continued fraction approximations.
Name: Maximum path sum II
Solution: 7273
Timing: 1.25228ms
Approach: Dijkstra's algorithm.
Name: Magic 5-gon ring
Solution: 6531031914842725
Timing: 540.762ms
Approach: Generate permutations, checking validity on the way.
Name: Totient maximum
Solution: 510510
Timing: 625.53ms
Approach: Euler's Totient formula, using memoization to speed up computation. Sieve of Eratosthenes for primes.
Name: Totient permutation
Solution: 8319823
Timing: 23181.5ms
Approach: Euler's Totient formula, using memoization to speed up computation. Sieve of Eratosthenes for primes. Check permutations by digits counts.
Name: Ordered fractions
Solution: 428570
Timing: 23.7682ms
Approach: Search over denominators, a little algebra allows us to check if is closer.
Name: Counting fractions
Solution: 303963552391
Timing: 639.53ms
Approach: Sum values of the totient function.
Name: Counting fractions in a range
Solution: 7295372
Timing: 114.229ms
Approach: Stern–Brocot tree.
Name: Digit factorial chains
Solution: 402
Timing: 23783.8ms
Approach: Direct search (could be improved with caching)
Name: Singular integer right triangles
Solution: 161667
Timing: 985.493ms
Approach: Euclid's formula for generating Pythagorean triples.
Name: Counting summations
Solution: 190569291
Timing: 0.083216ms
Approach: Dynamic programming, copy of problem 31.
Name: Prime summations
Solution: 71
Timing: 0.4757ms
Approach: Dynamic programming with a table, also sieve of Eratosthenes for primes.
Name: Coin partitions
Solution: 55374
Timing: 10713ms
Approach: Euler's formula for the partition function.
Name: Passcode derivation
Solution: 73162890
Timing: 0.691495ms
Approach: Assuming each digit is used only once, we can construct a graph from the number orderings, then find the unique topological ordering that is also a Hamiltonian path by iteratively removing the node with no children.
Name: Square root digital expansion
Solution: 40886
Timing: 3.37375ms
Approach: The GNU multi-precision library makes this straight-forward.
Name: Path sum: two ways
Solution: 427337
Timing: 1.87716
Approach: Dijkstra's algorithm (Same as problem 67).
Name: Path sum: three ways
Solution: 260324
Timing: 2.00656ms
Approach: Dijkstra's algorithm (Same as problem 81).
Name: Path sum: four ways
Solution: 425185
Timing: 110.634ms
Approach: Full Dijkstra's algorithm with a priority queue.
Name: Monopoly odds
Solution: 101524
Timing: 785.269ms
Approach: Monte-Carlo simulation.
Name: Counting rectangles
Solution: 2772
Timing: 0.240589ms
Approach: Direct brute force search.
Name: Cuboid route
Solution: 1818
Timing: 9576.07ms
Approach: Brute force search. (Faster is possible with Euclid's Pythagorean triple generating function)
Name: Prime power triples
Solution: 1097343
Timing: 2035.74ms
Approach: Brute force with appropriate choices of loops and bounds.
Name: Product-sum numbers
Solution: 7587457
Timing: 2540.85ms
Approach: Dynamic programming.
Name: Roman numerals
Solution: 743
Timing: 6.53083ms
Approach: Just parsing.
Name: Cube digit pairs
Solution: 1217
Timing: 1718.46ms
Approach: Brute force.
Name: Right triangles with integer coordinates
Solution: 14234
Timing: 0.221873ms
Approach: Because a right-angle between lines means they have negative reciprocal slopes we can efficiently look at all points in the lattice and count points that make right triangles with them.
Name: Square digit chains
Solution: 8581146
Timing: 24174.3ms
Approach: Direct approach, caching previous results.
Name: Arithmetic expressions
Solution: 1258
Timing: 1143.05ms
Approach: Brute force iterate over possible expression trees.
Name: Almost equilateral triangles
Solution: 518408346
Timing: 34173.8ms
Approach: Euclid's method for generating Pythagorean triplets.
Name: Amicable chains
Solution: 14316
Timing: 3896.47ms
Approach: Cache previous chains, use fast method for Aliquot sum from problem 23.
Name: Su Doku
Solution: 24702
Timing: 3264.59ms
Approach: Keep track of possibilities for each cell. Search with backtracking.
Name: Large non-Mersenne prime
Solution: 8739992577
Timing: 0.060778ms
Approach: Fast exponentiation.
Name: Anagramic squares
Solution: 18769
Timing: 1130.15ms
Approach: Create sets of permutation words, use permutations of square numbers to map to the permutations.
Name: Largest exponential
Solution: 709
Timing: 2.38343ms
Approach: Use logarithms to make the sizes fit into memory. Turns out we have enough precision for this approach to work.
Name: Arranged probability
Solution: 756872327473
Timing: 2.97521ms
Approach: This problems reduces to Pell's equation. See problem 66.
- https://sites.google.com/site/indy256/algo_cpp/bigint
- https://gmplib.org/manual/C_002b_002b-Interface-Integers#C_002b_002b-Interface-Integers
- https://en.wikipedia.org/wiki/GNU_Multiple_Precision_Arithmetic_Library
- 80
- https://en.wikipedia.org/wiki/Fibonacci_number#Binet's_formula
- 2, 25
- https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
- 3, 7, 10, 12, 21, 23, 27, 35, 37, 41, 46, 47, 49, 50, 58, 60, 69, 70, 77
- https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
- 18, 67, 81, 82, 83, 87
- https://en.wikipedia.org/wiki/Triangular_number
- 6, 12, 28, 42
- https://en.wikipedia.org/wiki/Square_pyramidal_number
- 6, 28
- https://en.wikipedia.org/wiki/Figurate_number
- 44, 45, 61
- https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)
- 15
- https://en.wikipedia.org/wiki/Geometric_series
- 21, 23
- https://en.wikipedia.org/wiki/Factorial_number_system
- 24
- https://en.wikipedia.org/wiki/Multiset
- 30
- https://en.wikipedia.org/wiki/Dynamic_programming
- 31, 76, 77, 88
- https://en.wikipedia.org/wiki/Permutation
- 32, 38, 41, 43, 98
- https://en.wikipedia.org/wiki/Euclidean_algorithm
- 33, 91, 94
- https://en.wikipedia.org/wiki/Pascal%27s_rule
- 53
- https://en.wikipedia.org/wiki/Addition
- 48, 55, 57
- https://en.wikipedia.org/wiki/Exponentiation_by_squaring
- 56, 97
- https://en.wikipedia.org/wiki/Lychrel_number
- 55
- https://en.wikipedia.org/wiki/Most_common_words_in_English
- 59
- https://en.wikipedia.org/wiki/XOR_cipher
- 59
- https://en.wikipedia.org/wiki/Clique_problem
- 60
- https://en.wikipedia.org/wiki/List_of_logarithmic_identities
- 63, 99
- https://en.wikipedia.org/wiki/Pell%27s_equation
- 66, 100
- https://en.wikipedia.org/wiki/Euler%27s_totient_function
- 69, 70, 72
- https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree
- 73
- https://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple
- 75, 86, 94
- https://en.wikipedia.org/wiki/Partition_function_(number_theory)
- 78
- https://en.wikipedia.org/wiki/Monte_Carlo_method
- 84
- https://en.wikipedia.org/wiki/Aliquot_sum
- 21, 23, 95
- https://en.wikipedia.org/wiki/Binary_expression_tree
- 93