Project Euler, step by step
Interactive walk-throughs of my solutions.
- PROBLEM 1Problem 1: Multiples of 3 or 5Watch sum_3_5 from euler_1.py scan the numbers below 10 (so 1 to 9). The real problem uses 1000; the mechanism is identical.
- PROBLEM 2Problem 2: Sum of Even Fibonacci TermsWalk the Fibonacci sequence below 100 the way euler_2.py does: every third term is even, so track a 3-step counter instead of checking parity directly.
- PROBLEM 3Problem 3: Largest Prime FactorWatch largest_prime_finder from euler_3.py divide 13195 down: find a divisor, divide it out completely, then search again on the smaller number until what is left is prime.
- PROBLEM 4Problem 4: Largest Palindrome ProductWatch largest_pal_in_range from euler_4.py try products of two numbers from 10 to 99 from the top down, stopping each row early once a product falls below the best palindrome so far.
- PROBLEM 5Problem 5: Smallest MultipleWatch smallest_multiple from euler_5.py build the answer as a list of prime factors: primes go straight in, then a pool of composites is divided down by known primes until only primes are left.
- PROBLEM 6Problem 6: Sum Square Differenceeuler_6.py never squares the sum. It adds every cross product x*y with x before y in the list, then doubles the total: in the table of all products that is the area below the diagonal, doubled.
- PROBLEM 7Problem 7: The 10001st PrimeWatch find_nth_prime from euler_7.py scan upward for the 6th prime, using a 6k+-1 test and skipping even candidates once past 3. The real problem wants the 10001st prime; the mechanism is identical.
- PROBLEM 8Problem 8: Highest-Product Run of Adjacent DigitsWatch give_answer from euler_8.py slide a 4-digit window along the number, skip every window that would cover a zero, and only multiply a window out when the digit entering it beats the digit that just left.
- PROBLEM 9Problem 9: Special Pythagorean Tripleteuler_9.py never searches over a, b, c. It uses Euclid's formula (a = m² − n², b = 2mn, c = m² + n²): the sides add to 2m(m + n), so from a given sum it only has to find a pair (m, n). Here the sum is 12.
- PROBLEM 10Sieve of Eratosthenes: sum of primes below two millionWatch the sieve from euler_10.py on the numbers below 100. The real problem uses 2,000,000; the mechanism is identical.
- PROBLEM 11Problem 11: Largest Product of 4 in a LineWatch euler_11.py's scan check every cell, in all 8 directions, for the highest product of 4 numbers in a straight line.
- PROBLEM 12Problem 12: Highly Divisible Triangular NumberWatch get_answer from euler_12.py test triangular numbers one at a time, using the n/(n+1) coprime trick to count divisors without factorizing the (much bigger) triangular number itself. Stops at the first T(n) with more than 5 divisors.
- PROBLEM 13Problem 13: First 10 Digits of a Large SumTruncate each of the hundred 50-digit numbers to its first 11 digits, sum the truncations, then truncate that sum to its first 10 digits -- using the real 100 numbers from euler_13.py directly.
- PROBLEM 14Problem 14: Longest Collatz ChainBuild the Collatz chain graph for starting numbers 1-14 with euler_14.py's get_graph, invert it, then BFS outward from 1 with longest_distance to find which starting number has the longest chain.
- PROBLEM 15Problem 15: Lattice Paths
- PROBLEM 16Problem 16: Digit Sum of a Power of TwoWatch get_digit_sum from euler_16.py peel the digits off 2^15 one at a time, then cross-check the total against get_sum_digit's string-based approach -- both on this small example and on the real 2^1000.
- PROBLEM 17Problem 17: Number Letter CountsWalk get_answer_1 and get_answer_2 from euler_17.py across every number from 1 to 1000, spelling each out and counting letters (no spaces or hyphens) -- checkpointed at numbers that show each branch of the naming logic.
- PROBLEM 18Problem 18: Maximum Path Sum (Pyramid)Watch euler_18.py's bottom-up collapse on the problem's own 15-row triangle: each cell takes itself plus the better of its two children below, until the top cell holds the best possible top-to-bottom path sum. The best route from the cell being combined is traced in green, and at the end one route from the top to the bottom is left.
- PROBLEM 19Problem 19: Sundays on the 1st of the MonthWalk euler_19.py's year-by-year advance: seed 1901's month-start day-positions from the known fact that 1 Jan 1900 was a Monday, then add 365 days per year (with a leap-year shift) and check which month-starts land on a Sunday.
- PROBLEM 20Problem 20: Digit Sum of a FactorialWatch get_digit_sum from euler_20.py peel the digits off 10! one at a time (ones place first) and sum them. The real problem asks for 100!; the mechanism is identical.
- PROBLEM 21Problem 21: Amicable PairsWatch euler_21.py's proper-divisor-sum sieve find amicable pairs in 1..300 -- the same mechanism that finds every pair below 10,000 in the real puzzle.
- PROBLEM 22Problem 22: Name ScoresWalk the first 12 names (alphabetically) from the real names file, scoring each as its 1-based position times the sum of its letter values, the way euler_22.py does.
- PROBLEM 23Problem 23: Non-Abundant SumsWatch euler_23.py's sieve-then-scan pipeline find every number below 100 that can't be written as a sum of two abundant numbers. The real problem uses 28123 -- the mechanism is identical.
- PROBLEM 24Problem 24: Lexicographic Permutationseuler_24.py has no code -- just a hand-worked comment deriving the millionth permutation of 0-9 via factorial-based digit elimination. This walk implements that same method from scratch and verifies it against a second, independent method.
- PROBLEM 25Problem 25: First 1000-Digit Fibonacci TermWalk euler_25.py's loop (a, b = b, a+b) and watch each term's digit count grow, stopping once a term reaches 3 digits. The real problem stops at 1000 digits instead.
- PROBLEM 26Problem 26: Longest Repeating Decimal CycleWatch is_repeating/get_k from euler_26.py (which import prime_factorize from euler_9) scan n = 1..10 for the fraction 1/n with the longest repeating decimal. The real puzzle scans 1..999.
- PROBLEM 27Problem 27: Quadratic PrimesSearch coefficients a, b of n^2 + an + b for the pair that produces the longest run of consecutive primes starting at n=0, calling the real get_consecutive_count/is_prime directly.
- PROBLEM 28Problem 28: Number Spiral DiagonalsWatch the four closed-form diagonal formulas from euler_28.py pick out the corner cells of a real 5x5 spiral (built the same way as the problem's own 5x5 diagram), ring by ring.
- PROBLEM 29Problem 29: Distinct PowersWatch get_29's skip-ahead optimization decide which a^b values are provable duplicates of an already-seen smaller base's powers, and check whether that skip ever throws away a genuinely new value.
- PROBLEM 30Problem 30: Digit Fifth PowersWatch is_sum_of_digits from euler_30.py walk each candidate's digits from the ones place up, summing fifth powers and bailing out early the moment the running total overshoots the number itself.
- PROBLEM 32Problem 32: Pandigital ProductsFind every a x b = c where the digits of a, b and c together use 1-9 exactly once, using the real is_pandigital check, at the real puzzle's full scale (no need to shrink -- the whole search is only a few thousand candidates).
- PROBLEM 33Problem 33: Curious FractionsScan every two-digit/two-digit fraction x=ab, c for the 'curious' cancelling property ab/bc == a/c, the same way euler_33.py does (full real range, only 89 values -- no need to shrink).
- PROBLEM 34Problem 34: Digit Factorialseuler_34.py tells its own bug-then-fix story: attempt_1 lets the number 2 sneak into the answer because its self-check never excludes single-digit numbers; attempt_2 fixes it by deleting that one key.
- PROBLEM 35Problem 35: Circular PrimesRotate every prime below 100 through all its digit positions (via euler_35.get_circle) and keep it only if every rotation is itself prime.
- PROBLEM 36Problem 36: Double-base PalindromesScan the numbers below 1000 from euler_36.py, keeping any that read the same forwards and backwards in BOTH base 10 and base 2. The real problem scans up to 1,000,000; the mechanism is identical.
- PROBLEM 37Problem 37: Truncatable Primeseuler_37.py's solve() has a real bug: it crashes and returns None on every run. This walk shows why, using the real remove_from_left/remove_from_right/is_contender functions, then verifies what the intended answer actually is.
Problems from Project Euler, licensed under CC BY-NC-SA 4.0.