Your search query "linkto%3A%22ProblemSets%2FProject Euler Solutions%22" didn't return any results. Please change some terms and refer to HelpOnSearching for more information.
(!) Consider performing a full-text search with your search terms.

Clear message

Python Solutions to Project Euler.


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:
            yield number   

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


    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:
                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:

    print max(factors(317584931803))

Unable to edit the page? See the FrontPage for instructions.