Python Implementation of Problem 10

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 10

This one was also relatively easy, but the problem I am running into is that my original implementation of primes() is not very fast. I did go back and refactor that, but I think I could do better. Maybe I will pass these through cython at some point.

Revision 1:

I took the lambda that was previously being used and replaced it with a partial() of an operator. This had a mild speed boost, and avoided using gross lambdas.

Revision 2:

Now it just references the __gt__ function directly

Problem:

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

Find the sum of all the primes below two million.

python.src.p0010.main() int
 1"""
 2Project Euler Problem 10
 3
 4This one was also relatively easy, but the problem I am running into is that my
 5original implementation of primes() is not very fast. I did go back and
 6refactor that, but I think I could do better. Maybe I will pass these through
 7cython at some point.
 8
 9Revision 1:
10
11I took the lambda that was previously being used and replaced it with a
12partial() of an operator. This had a mild speed boost, and avoided using gross
13lambdas.
14
15Revision 2:
16
17Now it just references the __gt__ function directly
18
19Problem:
20
21The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
22
23Find the sum of all the primes below two million.
24"""
25from itertools import takewhile
26
27from .lib.primes import primes
28
29
30def main() -> int:
31    return sum(takewhile((2_000_000).__gt__, primes()))

Tags: prime-number, python-iterator