Skip to content

Commit f1a50d1

Browse files
MaximSmolskiygithub-actionspre-commit-ci[bot]
authored andcommitted
Add Project Euler problem 800 solution 1 (TheAlgorithms#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>
1 parent f0b5b4f commit f1a50d1

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Diff for: DIRECTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@
317317
* [Longest Sub Array](dynamic_programming/longest_sub_array.py)
318318
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
319319
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
320+
* [Max Product Subarray](dynamic_programming/max_product_subarray.py)
320321
* [Max Sub Array](dynamic_programming/max_sub_array.py)
321322
* [Max Sum Contiguous Subsequence](dynamic_programming/max_sum_contiguous_subsequence.py)
322323
* [Min Distance Up Bottom](dynamic_programming/min_distance_up_bottom.py)
@@ -1016,6 +1017,8 @@
10161017
* [Sol1](project_euler/problem_587/sol1.py)
10171018
* Problem 686
10181019
* [Sol1](project_euler/problem_686/sol1.py)
1020+
* Problem 800
1021+
* [Sol1](project_euler/problem_800/sol1.py)
10191022

10201023
## Quantum
10211024
* [Bb84](quantum/bb84.py)

Diff for: project_euler/problem_800/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_800/sol1.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Project Euler Problem 800: https://projecteuler.net/problem=800
3+
4+
An integer of the form p^q q^p with prime numbers p != q is called a hybrid-integer.
5+
For example, 800 = 2^5 5^2 is a hybrid-integer.
6+
7+
We define C(n) to be the number of hybrid-integers less than or equal to n.
8+
You are given C(800) = 2 and C(800^800) = 10790
9+
10+
Find C(800800^800800)
11+
"""
12+
13+
from math import isqrt, log2
14+
15+
16+
def calculate_prime_numbers(max_number: int) -> list[int]:
17+
"""
18+
Returns prime numbers below max_number
19+
20+
>>> calculate_prime_numbers(10)
21+
[2, 3, 5, 7]
22+
"""
23+
24+
is_prime = [True] * max_number
25+
for i in range(2, isqrt(max_number - 1) + 1):
26+
if is_prime[i]:
27+
for j in range(i**2, max_number, i):
28+
is_prime[j] = False
29+
30+
return [i for i in range(2, max_number) if is_prime[i]]
31+
32+
33+
def solution(base: int = 800800, degree: int = 800800) -> int:
34+
"""
35+
Returns the number of hybrid-integers less than or equal to base^degree
36+
37+
>>> solution(800, 1)
38+
2
39+
40+
>>> solution(800, 800)
41+
10790
42+
"""
43+
44+
upper_bound = degree * log2(base)
45+
max_prime = int(upper_bound)
46+
prime_numbers = calculate_prime_numbers(max_prime)
47+
48+
hybrid_integers_count = 0
49+
left = 0
50+
right = len(prime_numbers) - 1
51+
while left < right:
52+
while (
53+
prime_numbers[right] * log2(prime_numbers[left])
54+
+ prime_numbers[left] * log2(prime_numbers[right])
55+
> upper_bound
56+
):
57+
right -= 1
58+
hybrid_integers_count += right - left
59+
left += 1
60+
61+
return hybrid_integers_count
62+
63+
64+
if __name__ == "__main__":
65+
print(f"{solution() = }")

0 commit comments

Comments
 (0)