create array with first 88 primes numbers in python

One way to do this is to use a function to check if numbers are prime, and then iterate through numbers until you have collected the first 88 prime numbers. Here is an example:

main.py
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

primes = []
num = 2
while len(primes) < 88:
    if is_prime(num):
        primes.append(num)
    num += 1

print(primes)
280 chars
17 lines

The is_prime() function checks if a number is prime using a basic trial division method. The loop then iterates through numbers starting at 2 until the first 88 prime numbers are collected. The resulting array is printed at the end.

related categories

gistlibby LogSnag