Add Project Euler problem 800 solution 1 (#8567)

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:
Maxim Smolskiy 2023-04-01 15:18:13 +03:00 committed by GitHub
parent 9e0c357a57
commit d66e1e8732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View File

@ -317,6 +317,7 @@
* [Longest Sub Array](dynamic_programming/longest_sub_array.py) * [Longest Sub Array](dynamic_programming/longest_sub_array.py)
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py) * [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py) * [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
* [Max Product Subarray](dynamic_programming/max_product_subarray.py)
* [Max Sub Array](dynamic_programming/max_sub_array.py) * [Max Sub Array](dynamic_programming/max_sub_array.py)
* [Max Sum Contiguous Subsequence](dynamic_programming/max_sum_contiguous_subsequence.py) * [Max Sum Contiguous Subsequence](dynamic_programming/max_sum_contiguous_subsequence.py)
* [Min Distance Up Bottom](dynamic_programming/min_distance_up_bottom.py) * [Min Distance Up Bottom](dynamic_programming/min_distance_up_bottom.py)
@ -1016,6 +1017,8 @@
* [Sol1](project_euler/problem_587/sol1.py) * [Sol1](project_euler/problem_587/sol1.py)
* Problem 686 * Problem 686
* [Sol1](project_euler/problem_686/sol1.py) * [Sol1](project_euler/problem_686/sol1.py)
* Problem 800
* [Sol1](project_euler/problem_800/sol1.py)
## Quantum ## Quantum
* [Bb84](quantum/bb84.py) * [Bb84](quantum/bb84.py)

View File

View File

@ -0,0 +1,65 @@
"""
Project Euler Problem 800: https://projecteuler.net/problem=800
An integer of the form p^q q^p with prime numbers p != q is called a hybrid-integer.
For example, 800 = 2^5 5^2 is a hybrid-integer.
We define C(n) to be the number of hybrid-integers less than or equal to n.
You are given C(800) = 2 and C(800^800) = 10790
Find C(800800^800800)
"""
from math import isqrt, log2
def calculate_prime_numbers(max_number: int) -> list[int]:
"""
Returns prime numbers below max_number
>>> calculate_prime_numbers(10)
[2, 3, 5, 7]
"""
is_prime = [True] * max_number
for i in range(2, isqrt(max_number - 1) + 1):
if is_prime[i]:
for j in range(i**2, max_number, i):
is_prime[j] = False
return [i for i in range(2, max_number) if is_prime[i]]
def solution(base: int = 800800, degree: int = 800800) -> int:
"""
Returns the number of hybrid-integers less than or equal to base^degree
>>> solution(800, 1)
2
>>> solution(800, 800)
10790
"""
upper_bound = degree * log2(base)
max_prime = int(upper_bound)
prime_numbers = calculate_prime_numbers(max_prime)
hybrid_integers_count = 0
left = 0
right = len(prime_numbers) - 1
while left < right:
while (
prime_numbers[right] * log2(prime_numbers[left])
+ prime_numbers[left] * log2(prime_numbers[right])
> upper_bound
):
right -= 1
hybrid_integers_count += right - left
left += 1
return hybrid_integers_count
if __name__ == "__main__":
print(f"{solution() = }")