This commit is contained in:
Reese Wells
2018-07-29 17:25:09 -04:00
commit 47c752ff65
6 changed files with 164 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
venv/
*.xlsx
*.docx
.idea/
__pycache__/

47
hardfindprime.py Normal file
View File

@@ -0,0 +1,47 @@
from timer import timing_function
def isprime(number, primelist):
"""
checks if a number is prime by finding dividing it by every other prime
below floor(number / 2)
"""
for prime in primelist:
if prime <= number // 2 and number % prime == 0:
return False
return True
@timing_function
def findprimesto(primecap):
"""
finds numprimes count of prime numbers
"""
primelist = [2]
testnum = 3
while testnum < primecap:
if isprime(testnum, primelist):
primelist.append(testnum)
testnum += 2
return primelist
@timing_function
def findxprimes(x):
"""
finds numprimes count of prime numbers
"""
primelist = [2]
testnum = 3
while len(primelist) < x:
if isprime(testnum, primelist):
primelist.append(testnum)
testnum += 2
return primelist

28
measure.py Normal file
View File

@@ -0,0 +1,28 @@
import simplefindprime as s, mediumfindprime as m, hardfindprime as h
message = "Time to run program: "
tests = 25
test_num = 1000
simpletime = 0
medtime = 0
hardtime = 0
for i in range(0, tests):
print("Test: " + str(i + 1))
simpletime += s.findxprimes(test_num)[1]
medtime += m.findxprimes(test_num)[1]
hardtime += h.findxprimes(test_num)[1]
simpletime /= tests
medtime /= tests
hardtime /= tests
print()
print(message + str(simpletime))
print(message + str(medtime))
print(message + str(hardtime))
print()
print("efficiency of medium vs simple: " + str(int(round(1/(medtime/simpletime),0))) + "x")
print("efficiency of hard vs medium: " + str(int(round(1/(hardtime/medtime), 0))) + "x")
print("efficiency of hard vs simple: " + str(int(round(1/(hardtime/simpletime), 0))) + "x")

38
mediumfindprime.py Normal file
View File

@@ -0,0 +1,38 @@
from timer import timing_function
def isprime(number):
"""
checks if a number is prime by dividing it into every odd number less than
number // 2
"""
current_num = 3
if number % 2 == 0:
return False
while current_num <= number // 2:
if number % current_num == 0:
return False
current_num += 2
return True
@timing_function
def findprimesto(primecap):
for i in range(0, primecap):
isprime(i)
@timing_function
def findxprimes(x):
primelist = []
current_num = 2
while len(primelist) < x:
if isprime(current_num):
primelist.append(current_num)
current_num += 1
return primelist

31
simplefindprime.py Normal file
View File

@@ -0,0 +1,31 @@
from timer import timing_function
def isprime(number):
"""
checks if a number is prime by dividing it into every number less than it
"""
for i in range(2,number):
if number % i == 0:
return False
return True
@timing_function
def findprimesto(primecap):
for i in range(0, primecap):
isprime(i)
@timing_function
def findxprimes(x):
primelist = []
current_num = 2
while len(primelist) < x:
if isprime(current_num):
primelist.append(current_num)
current_num += 1
return primelist

15
timer.py Normal file
View File

@@ -0,0 +1,15 @@
import time
def timing_function(prime_finder, *args):
"""
Outputs the time a function takes
to execute.
"""
def wrapper(*args):
t1 = time.time()
result = prime_finder(*args)
t2 = time.time()
return result, t2 - t1
return wrapper