diff --git a/PrimePython/solution_4/Dockerfile b/PrimePython/solution_4/Dockerfile new file mode 100644 index 000000000..6bf2b6b45 --- /dev/null +++ b/PrimePython/solution_4/Dockerfile @@ -0,0 +1,12 @@ +# For more information, please refer to https://aka.ms/vscode-docker-python +FROM python:3.9-slim-buster + +# Install pip requirements +RUN python -m pip install numba +RUN python -m pip install numpy + +WORKDIR /opt/app +COPY PrimePY.py . + +# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug +ENTRYPOINT ["python3", "PrimePY.py"] \ No newline at end of file diff --git a/PrimePython/solution_4/PrimePY.py b/PrimePython/solution_4/PrimePY.py new file mode 100644 index 000000000..86a243dfa --- /dev/null +++ b/PrimePython/solution_4/PrimePY.py @@ -0,0 +1,78 @@ +""" +Python Prime Sieve using Numpy and Numba +Based on MyFirstPython Program (tm) Dave Plummer 8/9/2018 + +Adapted by Joshua Estes 08/12/2021 + +This version is nearly a copy from PrimePython/solution3 but adapted to the Numba Python Compiler. +""" + +from numba import njit +import numpy as np +from math import sqrt +import time + +prime_numbers = { + 10: 4, + 100: 25, + 1000: 168, + 10000: 1229, + 100000: 9592, + 1000000: 78498, + 10000000: 664579, + 100000000: 5761455 +} + + +@njit(fastmath=True) +def run_sieve(size): + bits = np.ones(((size + 1) // 2), dtype=np.bool_) + factor = 1 + q = sqrt(size) / 2 + bits_view = bits[factor:] + while factor <= q: + for v in bits_view.flat: + if v: + break + factor += 1 + bits_view = bits[factor + 1:] + factor2 = 2 * factor + start = factor2 * (factor + 1) - factor - 1 + step = factor2 + 1 + bits_view[start::step] = False + factor += 1 + + +def count_primes(size): + if size < 2: + return 0 + return np.sum(np.ones(((size + 1) // 2), dtype=np.bool_)) + + +def get_primes(size): + if size < 2: + return tuple() + primes = np.where(np.ones(((size + 1) // 2), dtype=np.bool_))[0] * 2 + 1 + primes[0] = 2 + return primes + + +def print_results(duration, passes, size): + print(f"Passes: {passes}, Time: {duration}, Avg: {duration / passes}, Limit: {size}, " + f"Count: {len(get_primes(size))}", end="\n") + print("jestes15_numpy/numba; %s; %s; 1; algorithm=base, faithful=no, bits=8" % (passes, duration)) + + +def run(): + run_sieve(100) # Give numba the function call to compile run_sieve + passes, size = 0, int(input("Input the highest number: ")) + iStart: float = time.time() + while (time.time() - iStart) < 5: + run_sieve(size) + passes += 1 + iEnd: float = time.time() - iStart + print_results(iEnd, passes, size) + + +if __name__ == "__main__": + run() diff --git a/PrimePython/solution_4/README.md b/PrimePython/solution_4/README.md new file mode 100644 index 000000000..9783ee05e --- /dev/null +++ b/PrimePython/solution_4/README.md @@ -0,0 +1,28 @@ +# Python solution by jestes15 + +![Algorithm](https://img.shields.io/badge/Algorithm-base-green) +![Faithfulness](https://img.shields.io/badge/Faithful-yes-green) +![Parallelism](https://img.shields.io/badge/Parallel-no-green) +![Bit count](https://img.shields.io/badge/Bits-unknown-yellowgreen) + +This is an unfaithful solution due to the inclusion of numpy and numba. Copied from solution 3 with some minor tune ups in the code. + +# Results on my machine + + - Intel i7-10759H, Windows 11 + - Python: 3.8.8 64 bit + + +Report with select other solutions: +``` + Single-threaded +┌───────┬────────────────┬──────────┬──────────────────────┬─────────┬──────────┬─────────┬───────────┬──────────┬──────┬───────────────┐ +│ Index │ Implementation │ Solution │ Label │ Passes │ Duration │ Threads │ Algorithm │ Faithful │ Bits │ Passes/Second │ +├───────┼────────────────┼──────────┼──────────────────────┼─────────┼──────────┼─────────┼───────────┼──────────┼──────┼───────────────┤ +│ 1 │ c │ 1 │ mckoss-c830 │ 12469 │ 5.00000 │ 1 │ wheel │ yes │ 1 │ 2493.80000 │ +│ 2 │ python │ 4 │ jestes15_numpy_numba │ 8503 │ 5.00100 │ 1 │ base │ no │ 8 │ 1700.60000 │ +│ 3 │ python │ 3 │ emillynge_numpy │ 7830 │ 5.00030 │ 1 │ base │ no │ 8 │ 1565.90684 │ +│ 4 │ python │ 2 │ ssovest │ 2179 │ 5.00037 │ 1 │ base │ yes │ 8 │ 435.76803 │ +│ 5 │ python │ 1 │ davepl │ 40 │ 10.05578 │ 1 │ base │ yes │ │ 3.97781 │ +└───────┴────────────────┴──────────┴──────────────────────┴─────────┴──────────┴─────────┴───────────┴──────────┴──────┴───────────────┘ +```