Please note: This wiki is currently running in test mode after an attack on January 5 2013. All passwords were reset, so you will have to use the password recovery function to get a new password. To edit wiki pages, please log in first. See the wiki attack description page for more details. If you find problems, please report them to the pydotorg-www mailing list.

Consider performing a full-text search with your search terms.

Clear message

Python Solutions to Project Euler.

Index

Solutions to the first 40 problems in functional Python

Just found this site which is apparently devoted to solutions for the Euler problem set, in python, with a functional flavor.

Problem 1: Add all the natural numbers below 1000 that are multiples of 3 or 5.

```    def multiples_of_3_or_5():
for number in xrange(1000):
if not number % 3 or not number % 5:
yield number

print sum(multiples_of_3_or_5())```

In one line:

`    print sum( number for number in xrange(1000) if not (number % 3 and number % 5) )`

Problem 2: Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed one million.

```    def fib():
x,y = 0,1
while True:
yield x
x,y = y, x+y

def even(seq):
for number in seq:
if not number % 2:
yield number

def under_a_million(seq):
for number in seq:
if number > 1000000:
break
yield number

print sum(even(under_a_million(fib())))```

Alternately:

```    import itertools

def fib():
x,y = 0,1
while True:
yield x
x,y = y, x+y

print sum(x for x in itertools.takewhile(lambda x: x <= 1000000, fib()) if x % 2 == 0)```

Problem 3: Find the largest prime factor of 317584931803.

```    plist = [2]

def primes(min, max):
if 2 >= min: yield 2
for i in range(3, max, 2):
for p in plist:
if i%p == 0 or p*p > i: break
if i%p:
plist.append(i)
if i >= min: yield i

def factors(number):
for prime in primes(2, number):
if number % prime == 0:
number /= prime
yield prime
if number == 1:
break

print max(factors(317584931803))```