Prime Number Generation in Python

  • Share this:

Code introduction


This function returns a list of all prime numbers less than or equal to the given number n.


Technology Stack : Built-in libraries

Code Type : Function

Code Difficulty : Intermediate


                
                    
def get_prime_numbers(n):
    if n < 2:
        return []

    primes = [2]
    for i in range(3, n + 1, 2):
        is_prime = all(i % p != 0 for p in primes if p * p <= i)
        if is_prime:
            primes.append(i)

    return primes