From 47c752ff65a9975b2d72f6ae4d5cf2d05510bc4f Mon Sep 17 00:00:00 2001 From: Reese Wells Date: Sun, 29 Jul 2018 17:25:09 -0400 Subject: [PATCH] init --- .gitignore | 5 +++++ hardfindprime.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ measure.py | 28 +++++++++++++++++++++++++++ mediumfindprime.py | 38 +++++++++++++++++++++++++++++++++++++ simplefindprime.py | 31 ++++++++++++++++++++++++++++++ timer.py | 15 +++++++++++++++ 6 files changed, 164 insertions(+) create mode 100644 .gitignore create mode 100644 hardfindprime.py create mode 100644 measure.py create mode 100644 mediumfindprime.py create mode 100644 simplefindprime.py create mode 100644 timer.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73d1716 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +venv/ +*.xlsx +*.docx +.idea/ +__pycache__/ diff --git a/hardfindprime.py b/hardfindprime.py new file mode 100644 index 0000000..8377e10 --- /dev/null +++ b/hardfindprime.py @@ -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 diff --git a/measure.py b/measure.py new file mode 100644 index 0000000..ef7b277 --- /dev/null +++ b/measure.py @@ -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") \ No newline at end of file diff --git a/mediumfindprime.py b/mediumfindprime.py new file mode 100644 index 0000000..11702b8 --- /dev/null +++ b/mediumfindprime.py @@ -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 diff --git a/simplefindprime.py b/simplefindprime.py new file mode 100644 index 0000000..dabb9d7 --- /dev/null +++ b/simplefindprime.py @@ -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 diff --git a/timer.py b/timer.py new file mode 100644 index 0000000..8b55b4b --- /dev/null +++ b/timer.py @@ -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