38 lines
No EOL
1.1 KiB
Text
38 lines
No EOL
1.1 KiB
Text
def sieve_of_eratosthenes(limit):
|
|
primes = []
|
|
is_prime = [True] * (limit + 1)
|
|
for num in range(2, limit + 1):
|
|
if is_prime[num]:
|
|
primes.append(num)
|
|
for multiple in range(num * num, limit + 1, num):
|
|
is_prime[multiple] = False
|
|
return primes
|
|
|
|
|
|
def is_prime_trial_division(n):
|
|
if n <= 1:
|
|
return False
|
|
if n <= 3:
|
|
return True
|
|
if n % 2 == 0 or n % 3 == 0:
|
|
return False
|
|
i = 5
|
|
while i * i <= n:
|
|
if n % i == 0 or n % (i + 2) == 0:
|
|
return False
|
|
i += 6
|
|
return True
|
|
|
|
|
|
# Calculate the first 1000 prime numbers
|
|
limit = 7920 # A rough estimate to ensure we get at least 1000 primes
|
|
primes = sieve_of_eratosthenes(limit)
|
|
first_1000_primes = primes[:1000]
|
|
|
|
# Validate the list of primes
|
|
validated_primes = [p for p in first_1000_primes if is_prime_trial_division(p)]
|
|
|
|
# Check if the validation was successful
|
|
assert len(validated_primes) == 1000, "Validation failed: Not all numbers are prime"
|
|
|
|
print("First 1000 primes calculated and validated successfully.") |