1. Prime Numbers

Function that returns a list of all prime numbers up to a given number. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers.

image

(as a beginner in Python, Mr. Mosh tutorials is very helpful and easy to understand. Thank You!)

1 Like

As beginner to understand the prime numbers code

def prime_numbers(num):
    lists = []
    for n in range(1, num+1):
        if n<=1:
            continue
        is_prime = True
        for i in range(2, int(n/2)+1):
            if n % i == 0:
                is_prime = False
                break
        if is_prime:
            lists.append(n)
    return lists
1 Like

Another way to return the prime numbers:

def list_primes(n):
    primes = []
    for i in range(2,n+1):
        count = 1
        for j in range(2, i+1):
            if i % j == 0:
                count += 1
        if count == 2:
            primes.append(i)
    return primes
1 Like