Skip to content

Instantly share code, notes, and snippets.

@Tehnix
Created October 17, 2012 20:58
Show Gist options
  • Save Tehnix/3908142 to your computer and use it in GitHub Desktop.
Save Tehnix/3908142 to your computer and use it in GitHub Desktop.
Comparing primality checking functions
"""
Compare different methods of checking for a numbers primality.
"""
import math
import time
import sys
def isPrime(n):
"""Check if a number is a prime number."""
if n == 2:
return True
for i in range(3, int(math.sqrt(n) + 1), 2):
if n % i == 0 and n != i:
return False
return True
def isPrime2(n):
"""Check if a number is a prime number."""
if n == 2:
return True
return (len([x for x in range(3, int(math.sqrt(n) + 1), 2) if n % x == 0]) == 0)
def isPrime3(n):
"""Check if a number is a prime number."""
if n == 2:
return True
return all(map(lambda x: n % x != 0, range(3, int(math.sqrt(n) + 1), 2)))
def findAverage(func, runs, primeNumber):
"""
Execute a function [runs] number of times, and
return the average execution time.
"""
avg = 0
for i in range(runs):
start = time.time()
func(primeNumber)
avg += time.time() - start
return avg / runs
def output(text):
"""For quickly switching between Python 2 and 3."""
print(text)
def main():
runs = 1000
primeNumber = 32416190071
testFunctions = {
'isPrime': isPrime,
'isPrime2': isPrime2,
'isPrime3': isPrime3
}
output("+------------------------------------------+")
output("| Using Python v. %-5s |" % sys.version.split(' ')[0])
output("+------------------------------------------+")
output("| Checking if %-28s |" % (str(primeNumber) + " is a prime"))
output("+------------------------------------------+")
output("| Number of runs: %-23s |" % runs)
output("+------------------------------------------+")
output("")
output("+------------------------------------------+")
output("| Function Name | Average time to execute |")
output("+------------------------------------------+")
for name, func in testFunctions.items():
output("| %-13s | %-24s |" % (name, findAverage(func, runs, primeNumber),))
output("+------------------------------------------+")
if __name__ == '__main__':
main()
# +------------------------------------------+
# | Using Python v. 3.3.0 |
# +------------------------------------------+
# | Checking if 32416190071 is a prime |
# +------------------------------------------+
# | Number of runs: 1000 |
# +------------------------------------------+
#
# +------------------------------------------+
# | Function Name | Average time to execute |
# +------------------------------------------+
# | isPrime | 0.013823300361633302 |
# | isPrime2 | 0.01397840428352356 |
# | isPrime3 | 0.028225141286849977 |
# +------------------------------------------+
# +------------------------------------------+
# | Using Python v. 2.7.2 |
# +------------------------------------------+
# | Checking if 32416190071 is a prime |
# +------------------------------------------+
# | Number of runs: 1000 |
# +------------------------------------------+
#
# +------------------------------------------+
# | Function Name | Average time to execute |
# +------------------------------------------+
# | isPrime | 0.00974343919754 |
# | isPrime3 | 0.0233035748005 |
# | isPrime2 | 0.0090259552002 |
# +------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment