Improve Project Euler problem 203 solution 1 (#4807)

This commit is contained in:
Maxim Smolskiy 2021-10-06 17:11:15 +03:00 committed by GitHub
parent a4d68d69f1
commit 629369a34f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -75,17 +75,15 @@ def get_primes_squared(max_number: int) -> list[int]:
>>> get_primes_squared(100)
[4, 9, 25, 49]
"""
max_prime = round(math.sqrt(max_number))
non_primes = set()
max_prime = math.isqrt(max_number)
non_primes = [False] * (max_prime + 1)
primes = []
for num in range(2, max_prime + 1):
if num in non_primes:
if non_primes[num]:
continue
counter = 2
while num * counter <= max_prime:
non_primes.add(num * counter)
counter += 1
for num_counter in range(num ** 2, max_prime + 1, num):
non_primes[num_counter] = True
primes.append(num ** 2)
return primes