Programming

Euler 6

So the other night I was a bit bored and decided to do something to pass the time. I first came across Project Euler a while ago, but had never gone further than problem #1. Boredom is a great motivator and I went through problems #2 thru #9 last night and I decided to post my solutions in search of better ones. Feel free to comment with your suggestions.

Project Euler’s Problem #6 statement is —

The sum of the squares of the first ten natural numbers is:

12+22++102=385. 1^2 + 2^2 + \cdots + 10^2 = 385.

The square of the sum of the first ten natural numbers is:

(1+2++10)2=552=3025. (1 + 2 + \cdots + 10)^2 = 55^2 = 3025.

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025385=26403025 - 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

The two sums have closed forms, so there is no need to iterate at all:

n = 100
total = n * (n + 1) // 2
sum_of_squares = n * (n + 1) * (2 * n + 1) // 6

print(total * total - sum_of_squares)

This performs a constant number of integer operations and prints 25164150.

Euler 5

So the other night I was a bit bored and decided to do something to pass the time. I first came across Project Euler a while ago, but had never gone further than problem #1. Boredom is a great motivator and I went through problems #2 thru #9 last night and I decided to post my solutions in search of better ones. Feel free to comment with your suggestions.

Project Euler’s Problem #5 statement is —

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

The number must be the least common multiple of every integer from 1 through 20. Python’s integer lcm implements exactly that operation:

from math import lcm

answer = 1
for number in range(2, 21):
    answer = lcm(answer, number)

print(answer)

This prints 232792560. Folding the LCM keeps only the prime powers required by the numbers seen so far, with no brute-force search.

Euler 4

So the other night I was a bit bored and decided to do something to pass the time. I first came across Project Euler a while ago, but had never gone further than problem #1. Boredom is a great motivator and I went through problems #2 thru #9 last night and I decided to post my solutions in search of better ones. Feel free to comment with your suggestions.

Project Euler’s Problem #4 statement is —

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009=91×999009 = 91 \times 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Searching downward lets us stop as soon as the remaining products cannot beat the best palindrome already found. Starting the inner loop at a also avoids checking both a * b and b * a.

largest = 0

for a in range(999, 99, -1):
    if a * a <= largest:
        break

    for b in range(a, 99, -1):
        product = a * b
        if product <= largest:
            break
        if str(product) == str(product)[::-1]:
            largest = product

print(largest)

This prints 906609 (993×913=906609993 \times 913 = 906\,609).

Euler 3

So the other night I was a bit bored and decided to do something to pass the time. I first came across Project Euler a while ago, but had never gone further than problem #1. Boredom is a great motivator and I went through problems #2 thru #9 last night and I decided to post my solutions in search of better ones. Feel free to comment with your suggestions.

Project Euler’s Problem #3 statement is —

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

There is no need to build a list of every divisor and test each one for primality. Instead, divide each factor out as soon as it is found. This makes the remaining number smaller throughout the search.

number = 600_851_475_143
largest_factor = 1

while number % 2 == 0:
    largest_factor = 2
    number //= 2

factor = 3
while factor * factor <= number:
    while number % factor == 0:
        largest_factor = factor
        number //= factor
    factor += 2

if number > 1:
    largest_factor = number

print(largest_factor)

After removing every possible factor up to the square root of the remaining number, anything left must itself be prime. The answer is 6857, found in constant space and at most square-root time.

Euler 8

So the other night I was a bit bored and decided to do something to pass the time. I first came across Project Euler a while ago, but had never gone further than problem #1. Boredom is a great motivator and I went through problems #2 thru #9 last night and I decided to post my solutions in search of better ones. Feel free to comment with your suggestions.

Project Euler’s Problem #8 statement is —

Find the greatest product of thirteen consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

A sliding window avoids multiplying the same twelve digits again for every position. Zeroes need a little care because they cannot be divided back out of the running product, so the window tracks how many it contains.

number = """\
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
"""

digits = [int(digit) for digit in "".join(number.split())]
window_size = 13
product = 1
zeroes = 0
largest = 0

for index, digit in enumerate(digits):
    if digit == 0:
        zeroes += 1
    else:
        product *= digit

    if index >= window_size:
        outgoing = digits[index - window_size]
        if outgoing == 0:
            zeroes -= 1
        else:
            product //= outgoing

    if index >= window_size - 1 and zeroes == 0:
        largest = max(largest, product)

print(largest)

Each digit enters and leaves the product once, making this linear-time. It prints 23514624000.