-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path007.py
More file actions
47 lines (33 loc) · 990 Bytes
/
007.py
File metadata and controls
47 lines (33 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Project Euler Problem 7
=======================
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 10001st prime number?
"""
from itertools import count
def is_prime(number):
"""
Takes a number and returns True if it's a prime number, otherwise returns False.
"""
if number == 2 or number == 3:
return True
if number <= 0 or number % 2 == 0 or number % 3 == 0:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
def prime_by_position(position):
"""
Takes an integer and returns the prime number at that position.
"""
prime_count = 0
for i in count(start=2):
if is_prime(i):
prime_count += 1
if prime_count == position:
return i
def test_prime_by_position():
assert prime_by_position(6) == 13
print(prime_by_position(10001))