from time import time

# Cherche les 400000 premiers nombres premiers avec le crible d'Eratosthene.
# La limite 6000000 est fixe: le 400000e nombre premier est 5800079.

begin = time()

target_count = 400000
limit = 6000000

is_composite = [False] * (limit + 1)
primes = [0] * target_count

candidate = 2
while candidate * candidate <= limit:
    if not is_composite[candidate]:
        multiple = candidate * candidate
        while multiple <= limit:
            is_composite[multiple] = True
            multiple += candidate
    candidate += 1

count = 0
checksum = 0
candidate = 2
while candidate <= limit and count < target_count:
    if not is_composite[candidate]:
        primes[count] = candidate
        checksum += candidate
        count += 1
    candidate += 1

last_prime = primes[count - 1]

print(count, "primes")
print("last =", last_prime)
print("checksum =", checksum)

end = time()
print(f"Duration = {int((end - begin) * 1000)} ms")
