Problem: Check whether the given number is prime or not.
Solution in Python:
import math
def is_prime(n):
"""
Author: Mayur P Srivastava
"""
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
max_possible_factor = int(math.ceil(math.sqrt(n)))
for i in range(3, max_possible_factor + 1, 2):
if n % i == 0:
return False
return True
Concepts learned: Single loop.
No comments:
Post a Comment