Skip to content

Commit adc3ccd

Browse files
MaximSmolskiygithub-actionspre-commit-ci[bot]
authored
Add Project Euler problem 131 solution 1 (TheAlgorithms#8179)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 47b3c72 commit adc3ccd

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

DIRECTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,14 @@
196196
* [Disjoint Set](data_structures/disjoint_set/disjoint_set.py)
197197
* Hashing
198198
* [Double Hash](data_structures/hashing/double_hash.py)
199+
* [Hash Map](data_structures/hashing/hash_map.py)
199200
* [Hash Table](data_structures/hashing/hash_table.py)
200201
* [Hash Table With Linked List](data_structures/hashing/hash_table_with_linked_list.py)
201202
* Number Theory
202203
* [Prime Numbers](data_structures/hashing/number_theory/prime_numbers.py)
203204
* [Quadratic Probing](data_structures/hashing/quadratic_probing.py)
205+
* Tests
206+
* [Test Hash Map](data_structures/hashing/tests/test_hash_map.py)
204207
* Heap
205208
* [Binomial Heap](data_structures/heap/binomial_heap.py)
206209
* [Heap](data_structures/heap/heap.py)
@@ -973,6 +976,8 @@
973976
* [Sol1](project_euler/problem_125/sol1.py)
974977
* Problem 129
975978
* [Sol1](project_euler/problem_129/sol1.py)
979+
* Problem 131
980+
* [Sol1](project_euler/problem_131/sol1.py)
976981
* Problem 135
977982
* [Sol1](project_euler/problem_135/sol1.py)
978983
* Problem 144

project_euler/problem_131/__init__.py

Whitespace-only changes.

project_euler/problem_131/sol1.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Project Euler Problem 131: https://projecteuler.net/problem=131
3+
4+
There are some prime values, p, for which there exists a positive integer, n,
5+
such that the expression n^3 + n^2p is a perfect cube.
6+
7+
For example, when p = 19, 8^3 + 8^2 x 19 = 12^3.
8+
9+
What is perhaps most surprising is that for each prime with this property
10+
the value of n is unique, and there are only four such primes below one-hundred.
11+
12+
How many primes below one million have this remarkable property?
13+
"""
14+
15+
from math import isqrt
16+
17+
18+
def is_prime(number: int) -> bool:
19+
"""
20+
Determines whether number is prime
21+
22+
>>> is_prime(3)
23+
True
24+
25+
>>> is_prime(4)
26+
False
27+
"""
28+
29+
for divisor in range(2, isqrt(number) + 1):
30+
if number % divisor == 0:
31+
return False
32+
return True
33+
34+
35+
def solution(max_prime: int = 10**6) -> int:
36+
"""
37+
Returns number of primes below max_prime with the property
38+
39+
>>> solution(100)
40+
4
41+
"""
42+
43+
primes_count = 0
44+
cube_index = 1
45+
prime_candidate = 7
46+
while prime_candidate < max_prime:
47+
primes_count += is_prime(prime_candidate)
48+
49+
cube_index += 1
50+
prime_candidate += 6 * cube_index
51+
52+
return primes_count
53+
54+
55+
if __name__ == "__main__":
56+
print(f"{solution() = }")

0 commit comments

Comments
 (0)