Improved prime checker + fixed typos

This commit is contained in:
kcotugno 2014-05-31 14:33:30 -07:00
parent 55326378c1
commit 4e06d2bed7

View File

@ -1,20 +1,26 @@
# Copyright (c) 2014 kcotugno # Copyright (C) 2014 kcotugno
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# LICENSE file or http://www.opensource.org/licenses/MIT # LICENSE file or http://www.opensource.org/licenses/MIT
# #
# Auther: kcotugno # Author: kcotugno
# Date 5/30/14 # Date 5/30/14
import math 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): def isprime(num):
if num < 2: if num == 2:
return True
if num < 2 or num % 2 == 0:
return False return False
for i in range(2, int(math.sqrt(num)) + 1): for i in range(3, int(math.sqrt(num)) + 1, 2):
if num % i == 0:
if num % i == 0:
return False return False
return True return True
@ -47,7 +53,7 @@ def perfection(calc):
def main(): def main():
print("Eucldean Perfection") print("Euclidean Perfection")
print("Copyright (C) 2014 kcotugno") print("Copyright (C) 2014 kcotugno")
print("Distributed under the MIT software license, see the accompanying") print("Distributed under the MIT software license, see the accompanying")
print("LICENSE file or http://www.opensource.org/licenses/MIT\n") print("LICENSE file or http://www.opensource.org/licenses/MIT\n")