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)
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)
In [3]:
from euler import Seq, FactorInteger, fst, timer
def p003():
return FactorInteger(600851475143) >> Seq.map(fst) >> Seq.max
timer(p003)
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)
In [5]:
from euler import Seq, LCM, timer
def p005():
return (range(1,21) >> Seq.reduce(LCM))
timer(p005)
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)
In [7]:
from euler import timer
def p006():
return sum(range(101)) ** 2 - sum(i ** 2 for i in range(101))
timer(p006)
In [8]:
from euler import prime, timer
def p007():
return prime(10000)
timer(p007)
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)
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)
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)
In [11]:
from euler import Seq, primes, timer
def p010():
return (
primes()
>> Seq.takeWhile(lambda n: n<2000000)
>> Seq.sum)
timer(p010)