Generating prime numbers using the sieve of eratosthenes algorithm

def sieve_of_eratosthenes(n):

    primes = [True] * (n + 1)

    primes[0] = primes[1] = False


    for i in range(2, int(n ** 0.5) + 1):

        if primes[i]:

            for j in range(i ** 2, n + 1, i):

                primes[j] = False


    return [i for i in range(2, n + 1) if primes[i]]


# Example usage

n = 100

primes = sieve_of_eratosthenes(n)

print("The prime numbers up to", n, "are:", primes)

No comments: