Write a function isPrime of type int -> bool that returns true if and only if its integer parameter is a prime number. Your function need not behave well if the parameter is negative.

Respuesta :

ijeggs

Answer:

import math

def isPrime(num):

 

   if num % 2 == 0 and num > 2:

       return False

   for i in range(3, int(math.sqrt(num)) + 1, 2):

       if num % i == 0:

           return False

   return True

Explanation:

The solution is provided in the python programming language, firstly the math class is imported so we can use the square root method. The first if statement checks if the number is even and greater than 2 and returns False since all even numbers except two are not prime numbers.

Then using a for loop on a range (3, int(math.sqrt(num)) + 1, 2), the checks if the number evenly divides through i and returns False otherwise it returns True

see code and output attached

Ver imagen ijeggs