Ad
Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Data Types
  1. Іf the number is even it is not prime (2 is an exception).
  2. There is no sence to check numbers that are greater than sqrt (num).
Code
Diff
  • import math
    
    def is_prime(num):
        if(not (num&1) and num != 2):
            return False
        for i in range(3, int(math.sqrt(num)) + 1, 2):
            if (num % i) == 0:
                return False
        return True
    
    def get_primes(max_num):
        return [i for i in range(2, max_num) if is_prime(i)]
    • import math
    • def is_prime(num):
    • for i in range(2, num):
    • if(not (num&1) and num != 2):
    • return False
    • for i in range(3, int(math.sqrt(num)) + 1, 2):
    • if (num % i) == 0:
    • return False
    • return True
    • def get_primes(max_num):
    • return [i for i in range(2, max_num) if is_prime(i)]