Posts (old posts, page 2)

Project Euler Problem 46: Goldbach's other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

$$ 9 = 7 + 2 \cdot 1^2 \\ 15 = 7 + 2 \cdot 2^2 \\ 21 = 3 + 2 \cdot 3^2 \\ 25 = 7 + 2 \cdot 3^2 \\ 27 = 19 + 2 \cdot 2^2 \\ 33 = 31 + 2 \cdot 1^2 $$

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

Version 1: Brute force

Formally stated, Goldbach's Other Conjecture says that all odd composite numbers $n$ can be expressed as

$$ n = 2k^2 + p $$

for some integer $k$ and prime number $p$. Let

$$ S_n = \{ n - 2k^2 : k = 1, 2, \dotsc, \lfloor \sqrt{\frac{n}{2}} \rfloor \} $$

If any element $n - 2k^2$ of $S_n$ is prime, then we call $k$ a witness to Goldbach's Other Conjecture for $n$. We are asked to find the smallest $n$ that has no witness, i.e. such that all elements of $S_n$ are composite.

Let $P_n$ be the set of all prime numbers stricly smaller than $n$, then our algorithm searches for the smallest $n$ such that $P_n \cap S_n = \emptyset$, providing a counterexample to Goldbach's Other Conjecture.

Read more…

Project Euler Problem 47: Distinct primes factors

The first two consecutive numbers to have two distinct prime factors are:

$$ 14 = 2 × 7 \\ 15 = 3 × 5 $$

The first three consecutive numbers to have three distinct prime factors are:

$$ 644 = 2² × 7 × 23 \\ 645 = 3 × 5 × 43 \\ 646 = 2 × 17 × 19. $$

Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?

Version 1: Fun times with Itertools

First let's load up the prime factorization function we implemented earlier. Recall that this function returns a Counter with the prime factor as the key, and its exponent as the value.

In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
from common.utils import prime_factors
In [3]:
from itertools import count, tee
from six.moves import map, reduce, zip

Define an $n$-wise iterator over an iterator, inspired by the implementation of pairwise in the Itertools recipes. It get $n$ iterators for the iterable, advances the $i$th iterator by $i$, and zips them back together.

In [4]:
def nwise(iterable, n=2):
    iters = tee(iterable, n)
    for i, it in enumerate(iters):
        for _ in range(i):
            next(it, None)
    return zip(*iters)

Project Euler Problem 48: Self powers

The series, $1^1 + 2^2 + 3^3 + ... + 10^{10} = 10405071317$.

Find the last ten digits of the series, $1^1 + 2^2 + 3^3 + ... + 1000^{1000}$.

Version 1: The obvious way

In [1]:
from six.moves import map, range, reduce
In [2]:
sum(map(lambda k: k**k, range(1, 1000+1))) % 10**10
Out[2]:
9110846700

Project Euler Problem 9: Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which,

\begin{equation} a^2 + b^2 = c^2 \end{equation}

For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$.

There exists exactly one Pythagorean triplet for which $a + b + c = 1000$. Find the product $abc$.

Remark

This is a fairly straighforward constraint satisfaction problem (CSP) and is perhaps most easily solved in a CSP modelling language such as MiniZinc. However, to employ such tools would be to defeat the very purpose of the exercise, which is to give us practice with implementation.

Read more…

Project Euler Problem 12: Highly divisible triangular number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

Read more…

Project Euler Problem 8: Largest product in a series

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

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

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

Read more…

Project Euler Problem 7: 10001st prime

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

Sieve of Eratosthenes

Previously, we implemented the Sieve of Eratosthenes. However, our implementation demands an integer $m$ and can only generate primes less than $m$. While some approximation algorithms for determining the $n$th prime are available, we would like to produce an exact solution. Hence, we must implement a prime sieve that does not require an upper bound.

In [1]:
from itertools import count, islice
from collections import defaultdict
In [2]:
def _sieve_of_eratosthenes():
    factors = defaultdict(set)
    for n in count(2):
        if factors[n]:
            for m in factors.pop(n):
                factors[n+m].add(m)
        else:
            factors[n*n].add(n)
            yield n
In [3]:
list(islice(_sieve_of_eratosthenes(), 20))
Out[3]:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]

Project Euler Problem 6: Sum square difference

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

$$1^2 + 2^2 + ... + 10^2 = 385$$

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

$$(1 + 2 + ... + 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 3025 − 385 = 2640.

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

Version 1

In [1]:
from six.moves import range
In [2]:
sum_sq_diff = lambda n: sum(range(1, n+1))**2 - sum(i**2 for i in range(1, n+1))
In [3]:
sum_sq_diff(10)
Out[3]:
2640
In [4]:
sum_sq_diff(100)
Out[4]:
25164150

Project Euler Problem 5: Smallest multiple

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?

In [1]:
from six.moves import range
In [2]:
all_divides = lambda m, *numbers: all(m % n == 0 for n in numbers)
In [3]:
all_divides(2520, *range(1, 10))
Out[3]:
True

The least common multiple of the numbers 1 to 10 is 2520. We are asked to find that of the numbers 1 to 20.

Version 1 - Integer Factorization

We already implemented lcm in problem 3, and the easiest way would be for us to simply use that. However, as we mentioned, it is not very efficient. Let's instead look at other ways of implementing it.

Version 2 - Simple algorithm

Given a list of $n$ integers $X = (x_1, x_2, \dotsc, x_n)$, we can find the least common multiple of the integers in $X$ by the following:

Let $X^{(0)} = (x_1^{(0)}, x_2^{(0)}, \dotsc, x_n^{(0)}) = (x_1, x_2, \dotsc, x_n) = X$ and $X^{(m+1)} = (x_1^{(m+1)}, x_2^{(m+1)}, \dotsc, x_n^{(m+1)})$ where

$$ x_k^{(m+1)} = x_k^{(m)} + \begin{cases} x_k^{(0)} & \text{if } \min(X^{(m)}) = x_k^{(m)} \\ 0 & \text{otherwise} \end{cases} $$

Once all $X$ are equal, in every entry is the LCM.

Read more…

Project Euler Problem 4: Largest palindrome product

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

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

In [1]:
from six.moves import filter, range
In [2]:
is_palindrome = lambda s: s == s[::-1]
num_is_palindrome = lambda n: is_palindrome(str(n))
In [3]:
max(filter(num_is_palindrome, (x*y for x in range(100, 1000) for y in range(x, 1000))))
Out[3]:
906609