mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
Add Project Euler problem 131 solution 1 (#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>
This commit is contained in:
parent
47b3c72982
commit
adc3ccdabe
|
@ -196,11 +196,14 @@
|
||||||
* [Disjoint Set](data_structures/disjoint_set/disjoint_set.py)
|
* [Disjoint Set](data_structures/disjoint_set/disjoint_set.py)
|
||||||
* Hashing
|
* Hashing
|
||||||
* [Double Hash](data_structures/hashing/double_hash.py)
|
* [Double Hash](data_structures/hashing/double_hash.py)
|
||||||
|
* [Hash Map](data_structures/hashing/hash_map.py)
|
||||||
* [Hash Table](data_structures/hashing/hash_table.py)
|
* [Hash Table](data_structures/hashing/hash_table.py)
|
||||||
* [Hash Table With Linked List](data_structures/hashing/hash_table_with_linked_list.py)
|
* [Hash Table With Linked List](data_structures/hashing/hash_table_with_linked_list.py)
|
||||||
* Number Theory
|
* Number Theory
|
||||||
* [Prime Numbers](data_structures/hashing/number_theory/prime_numbers.py)
|
* [Prime Numbers](data_structures/hashing/number_theory/prime_numbers.py)
|
||||||
* [Quadratic Probing](data_structures/hashing/quadratic_probing.py)
|
* [Quadratic Probing](data_structures/hashing/quadratic_probing.py)
|
||||||
|
* Tests
|
||||||
|
* [Test Hash Map](data_structures/hashing/tests/test_hash_map.py)
|
||||||
* Heap
|
* Heap
|
||||||
* [Binomial Heap](data_structures/heap/binomial_heap.py)
|
* [Binomial Heap](data_structures/heap/binomial_heap.py)
|
||||||
* [Heap](data_structures/heap/heap.py)
|
* [Heap](data_structures/heap/heap.py)
|
||||||
|
@ -973,6 +976,8 @@
|
||||||
* [Sol1](project_euler/problem_125/sol1.py)
|
* [Sol1](project_euler/problem_125/sol1.py)
|
||||||
* Problem 129
|
* Problem 129
|
||||||
* [Sol1](project_euler/problem_129/sol1.py)
|
* [Sol1](project_euler/problem_129/sol1.py)
|
||||||
|
* Problem 131
|
||||||
|
* [Sol1](project_euler/problem_131/sol1.py)
|
||||||
* Problem 135
|
* Problem 135
|
||||||
* [Sol1](project_euler/problem_135/sol1.py)
|
* [Sol1](project_euler/problem_135/sol1.py)
|
||||||
* Problem 144
|
* Problem 144
|
||||||
|
|
0
project_euler/problem_131/__init__.py
Normal file
0
project_euler/problem_131/__init__.py
Normal file
56
project_euler/problem_131/sol1.py
Normal file
56
project_euler/problem_131/sol1.py
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
"""
|
||||||
|
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?
|
||||||
|
"""
|
||||||
|
|
||||||
|
from math import isqrt
|
||||||
|
|
||||||
|
|
||||||
|
def is_prime(number: int) -> bool:
|
||||||
|
"""
|
||||||
|
Determines whether number is prime
|
||||||
|
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> is_prime(4)
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
|
||||||
|
for divisor in range(2, isqrt(number) + 1):
|
||||||
|
if number % divisor == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def solution(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
|
||||||
|
while prime_candidate < max_prime:
|
||||||
|
primes_count += is_prime(prime_candidate)
|
||||||
|
|
||||||
|
cube_index += 1
|
||||||
|
prime_candidate += 6 * cube_index
|
||||||
|
|
||||||
|
return primes_count
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"{solution() = }")
|
Loading…
Reference in New Issue
Block a user