Multiples of 3 and 5

Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.


In [1]:
from euler import Seq, timer

def p001():
    return (
        range(1000)
        >> Seq.filter(lambda n: (n%3==0) | (n%5==0))
        >> Seq.sum)

timer(p001)


result: 233168 (0.00s)

Even Fibonacci numbers

Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.


In [2]:
from euler import Seq, timer

def p002():
    return (
        Seq.unfold(lambda (a,b): (b, (b, b+a)), (0,1))
        >> Seq.filter(lambda n: n%2==0)
        >> Seq.takeWhile(lambda n: n<4000000)
        >> Seq.sum)    
    
timer(p002)


result: 4613732 (0.00s)

Largest prime factor

Problem 3

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

What is the largest prime factor of the number 600851475143 ?


In [3]:
from euler import Seq, FactorInteger, fst, timer

def p003():
    return FactorInteger(600851475143) >> Seq.map(fst) >> Seq.max

timer(p003)


result: 6857 (0.00s)

Largest palindrome product

Problem 4

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 [ ]:
from euler import Seq, timer

def p004():
    return (
        range(100, 1000)
        >> Seq.collect(lambda a: range(a, 1000)
                                 >> Seq.filter(lambda b: str(a*b)[::-1] == str(a*b))
                                 >> Seq.map(lambda b: a*b))
        >> Seq.max)

timer(p004)

In [4]:
from euler import Seq, timer

def p004():
    return (
        [a*b for a in range(100, 1000)
         for b in range(a, 1000)
         if str(a*b)[::-1] == str(a*b)]
        >> Seq.max)

timer(p004)


result: 906609 (0.25s)

Smallest multiple

Problem 5

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 [5]:
from euler import Seq, LCM, timer

def p005():
    return (range(1,21) >> Seq.reduce(LCM))

timer(p005)


result: 232792560 (0.00s)

Sum square difference

Problem 6

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

$12+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=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.


In [1]:
from euler import timer, Seq

def p006():
    return (range(101) >> Seq.sum) ** 2 - (range(101) >> Seq.sumBy(lambda i: i**2))

timer(p006)


result: 25164150 (0.00s)

In [7]:
from euler import timer

def p006():
    return sum(range(101)) ** 2 - sum(i ** 2 for i in range(101))

timer(p006)


result: 25164150 (0.00s)

10001st prime

Problem 7

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 10 001st prime number?


In [8]:
from euler import prime, timer

def p007():
    return prime(10000)

timer(p007)


result: 104743 (0.02s)

Largest product in a series

Problem 8

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


In [9]:
from euler import Seq, timer

def p007():
    return (
        "".join(open('data/p008.txt').read().splitlines())
        >> Seq.window(5)
        >> Seq.map(lambda s: s >> Seq.map(int) >> Seq.product)
        >> Seq.max)

timer(p007)


result: 40824 (0.01s)

Special Pythagorean triplet

Problem 9

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

$a^2+b^2=c^2$ 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$.


In [2]:
from euler import timer, Seq

def p009():
    return(
        range(1,999)
        >> Seq.collect(lambda a: range(a, 1000-a) 
                                 >> Seq.filter(lambda b: (a**2 + b**2 == (1000-a-b)**2)) 
                                 >> Seq.map(lambda b: a*b*(1000-a-b)))
        >> Seq.head)

timer(p009)


result: 31875000 (0.08s)

In [3]:
def p009():
    return (
        [a*b*(1000-a-b)
         for a in range(1,999) 
         for b in range(a, 1000-a)
         if (a ** 2 + b ** 2 == (1000-a-b) ** 2)][0])

timer(p009)


result: 31875000 (0.11s)

Summation of primes

Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.


In [11]:
from euler import Seq, primes, timer

def p010():
    return (
        primes()
        >> Seq.takeWhile(lambda n: n<2000000)
        >> Seq.sum)

timer(p010)


result: 142913828922 (0.24s)