From 4e06d2bed7347b62a88c297de07e94b35d1ad558 Mon Sep 17 00:00:00 2001 From: kcotugno Date: Sat, 31 May 2014 14:33:30 -0700 Subject: [PATCH] Improved prime checker + fixed typos --- euclideanp.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/euclideanp.py b/euclideanp.py index d00b6cc..f61b6bb 100644 --- a/euclideanp.py +++ b/euclideanp.py @@ -1,20 +1,26 @@ -# Copyright (c) 2014 kcotugno +# Copyright (C) 2014 kcotugno # Distributed under the MIT software license, see the accompanying # LICENSE file or http://www.opensource.org/licenses/MIT # -# Auther: kcotugno +# Author: kcotugno # Date 5/30/14 import math +# This function check if a number is prime first by testing for the #2, then if +# it less than 2 or divisible by 2. If non of these pre-checks return, it runs +# through a loop which goes from 3 to the sqrt of the number only testing it +# against odd numbers. def isprime(num): - if num < 2: + if num == 2: + return True + + if num < 2 or num % 2 == 0: return False - for i in range(2, int(math.sqrt(num)) + 1): - - if num % i == 0: + for i in range(3, int(math.sqrt(num)) + 1, 2): + if num % i == 0: return False return True @@ -47,7 +53,7 @@ def perfection(calc): def main(): - print("Eucldean Perfection") + print("Euclidean Perfection") print("Copyright (C) 2014 kcotugno") print("Distributed under the MIT software license, see the accompanying") print("LICENSE file or http://www.opensource.org/licenses/MIT\n")