Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions PrimePython/solution_4/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
78 changes: 78 additions & 0 deletions PrimePython/solution_4/PrimePY.py
Original file line number Diff line number Diff line change
@@ -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()
28 changes: 28 additions & 0 deletions PrimePython/solution_4/README.md
Original file line number Diff line number Diff line change
@@ -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 │
└───────┴────────────────┴──────────┴──────────────────────┴─────────┴──────────┴─────────┴───────────┴──────────┴──────┴───────────────┘
```