Python Implementation of Problem 7

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 7

I was able to chain this with a previous problem. Probably a suboptimal solution because of it, but it felt prettier this way.

I was able to add a short-circuited fail case to the is_prime() method, though

Revision 1:

Add a shortcut on is_prime to check the pfs cache first, and refactored to work with the new pfs object and name

Revision 2:

Add a count to the trigger on prime numbers

Revision 3:

Add a stop parameter to primes()

Revision 4:

Switch to takewhile, use prime cache again

Revision 5:

Move primes() to p0003 in order to fix caching

Problem:

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?

python.src.p0007.main() int
 1"""
 2Project Euler Problem 7
 3
 4I was able to chain this with a previous problem. Probably a suboptimal
 5solution because of it, but it felt prettier this way.
 6
 7I was able to add a short-circuited fail case to the is_prime() method, though
 8
 9Revision 1:
10
11Add a shortcut on is_prime to check the pfs cache first, and refactored to work
12with the new pfs object and name
13
14Revision 2:
15
16Add a count to the trigger on prime numbers
17
18Revision 3:
19
20Add a stop parameter to primes()
21
22Revision 4:
23
24Switch to takewhile, use prime cache again
25
26Revision 5:
27
28Move primes() to p0003 in order to fix caching
29
30Problem:
31
32By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
33the 6th prime is 13.
34
35What is the 10 001st prime number?
36"""
37from .lib.primes import primes
38
39
40def main() -> int:
41    for idx, num in enumerate(primes(), 1):
42        if idx == 10001:
43            return num
44    return -1  # pragma: no cover

Tags: prime-number, python-iterator