Skip to content

Commit ad1e62a

Browse files
Add solution for Problem 810
1 parent d88cb32 commit ad1e62a

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

project_euler/problem_810/sol1.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
2828
"""
2929

30+
from array import array
31+
3032

3133
def xor_multiply(op_a: int, op_b: int) -> int:
3234
"""
@@ -108,23 +110,17 @@ def find_xor_prime(rank: int) -> int:
108110
41
109111
"""
110112
total, degree = 0, 1
111-
while True:
112-
count = count_irreducibles(degree)
113-
if total + count > rank:
114-
break
115-
total += count
113+
while total + count_irreducibles(degree) < rank:
114+
total += count_irreducibles(degree)
116115
degree += 1
117116

118117
limit = 1 << (degree + 1)
119118

120-
sieve = [True] * limit
121-
sieve[0] = sieve[1] = False
122-
123-
for even in range(4, limit, 2):
124-
sieve[even] = False
119+
sieve = array("B", [1]) * limit
120+
sieve[0] = sieve[1] = 0
125121

126-
current = 1
127-
for i in range(3, limit, 2):
122+
current = 0
123+
for i in range(2, limit):
128124
if sieve[i]:
129125
current += 1
130126
if current == rank:
@@ -135,12 +131,10 @@ def find_xor_prime(rank: int) -> int:
135131
prod = xor_multiply(i, j)
136132
if prod >= limit:
137133
break
138-
sieve[prod] = False
139-
j += 2
134+
sieve[prod] = 0
135+
j += 1
140136

141-
raise ValueError(
142-
"Failed to locate the requested XOR-prime within the computed limit"
143-
)
137+
raise ValueError("Failed to locate the requested XOR-prime")
144138

145139

146140
def solution(limit: int = 5000001) -> int:

0 commit comments

Comments
 (0)