forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol1.py
53 lines (35 loc) · 1.17 KB
/
sol1.py
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
48
49
50
51
52
53
"""
Project Euler Problem 131: https://projecteuler.net/problem=131
There are some prime values, p, for which there exists a positive integer, n,
such that the expression n^3 + n^2p is a perfect cube.
For example, when p = 19, 8^3 + 8^2 x 19 = 12^3.
What is perhaps most surprising is that for each prime with this property
the value of n is unique, and there are only four such primes below one-hundred.
How many primes below one million have this remarkable property?
"""
frommathimportisqrt
defis_prime(number: int) ->bool:
"""
Determines whether number is prime
>>> is_prime(3)
True
>>> is_prime(4)
False
"""
returnall(number%divisor!=0fordivisorinrange(2, isqrt(number) +1))
defsolution(max_prime: int=10**6) ->int:
"""
Returns number of primes below max_prime with the property
>>> solution(100)
4
"""
primes_count=0
cube_index=1
prime_candidate=7
whileprime_candidate<max_prime:
primes_count+=is_prime(prime_candidate)
cube_index+=1
prime_candidate+=6*cube_index
returnprimes_count
if__name__=="__main__":
print(f"{solution() =}")