From 20407577511da192b6a08f103269dea0efbd8daf Mon Sep 17 00:00:00 2001 From: dheerajreddymumma Date: Sun, 28 Oct 2018 16:53:56 +0530 Subject: [PATCH 1/2] Optimized prime check code in Python. --- maths/PrimeCheck.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/maths/PrimeCheck.py b/maths/PrimeCheck.py index 79fd343db..1a543873a 100644 --- a/maths/PrimeCheck.py +++ b/maths/PrimeCheck.py @@ -1,12 +1,8 @@ +import math def primeCheck(number): - prime = True - for i in range(2, int(number**(0.5)+1), 2): - if i != 2: - i = i - 1 - if number % i == 0: - prime = False - break - return prime + if n % 2 == 0 and n > 2: + return False + return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2)) def main(): print(primeCheck(37)) From 1305e74b8ae28a81c0bdf9ee3a06796c97e66c55 Mon Sep 17 00:00:00 2001 From: dheerajreddymumma Date: Sun, 28 Oct 2018 17:02:27 +0530 Subject: [PATCH 2/2] New modified Python code for Prime check --- maths/PrimeCheck.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/PrimeCheck.py b/maths/PrimeCheck.py index 1a543873a..e0c51d77a 100644 --- a/maths/PrimeCheck.py +++ b/maths/PrimeCheck.py @@ -1,8 +1,8 @@ import math def primeCheck(number): - if n % 2 == 0 and n > 2: + if number % 2 == 0 and number > 2: return False - return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2)) + return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) def main(): print(primeCheck(37))