Skip to content

Commit b6ce75b

Browse files
committed
initial commit
0 parents  commit b6ce75b

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed

Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:latest
2+
3+
COPY bin/ /app
4+
5+
WORKDIR /app
6+
7+
ENTRYPOINT ["pydoc", "-w"]
8+
9+
CMD ["main"]

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# python-cli-pydoc-sieve-atkins
2+
3+
## Description
4+
Given a number find all primes.
5+
POC for pydoc.
6+
7+
## Tech stack
8+
- python
9+
- pydoc
10+
11+
## Docker stack
12+
- python:latest
13+
14+
## To run
15+
`sudo ./install.sh -u`
16+
17+
## To stop (optional)
18+
`sudo ./install.sh -d`
19+
20+
## For help
21+
`sudo ./install.sh -h`
22+
23+
## python-cli specific search
24+
- [Search by pydoc](https://github.com/bearddan2000?tab=repositories&q=python-cli-pydoc&type=&language=&sort=)
25+
- [Search by sieve](https://github.com/bearddan2000?tab=repositories&q=python-cli-sieve&type=&language=&sort=)
26+
- [Search by atkins](https://github.com/bearddan2000?tab=repositories&q=python-cli-atkins&type=&language=&sort=)
27+
28+
## General search
29+
- [Search by python](https://github.com/bearddan2000?tab=repositories&q=python&type=&language=&sort=)
30+
- [Search by cli](https://github.com/bearddan2000?tab=repositories&q=cli&type=&language=&sort=)
31+
- [Search by pydoc](https://github.com/bearddan2000?tab=repositories&q=pydoc&type=&language=&sort=)
32+
- [Search by sieve](https://github.com/bearddan2000?tab=repositories&q=sieve&type=&language=&sort=)
33+
- [Search by atkins](https://github.com/bearddan2000?tab=repositories&q=atkins&type=&language=&sort=)

bin/main.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Python 3 program for
2+
# implementation of
3+
# Sieve of Atkin
4+
5+
def SieveOfAtkin(limit):
6+
"""
7+
Sieve based on Atkin algorythm.
8+
:param limit: number of maximum range
9+
:return:
10+
"""
11+
12+
# 2 and 3 are known
13+
# to be prime
14+
if (limit > 2):
15+
print(2 , end = " ")
16+
if (limit > 3):
17+
print(3 , end = " ")
18+
19+
# Initialise the sieve
20+
# array with False values
21+
sieve = [False] * (limit + 1)
22+
for i in range( 0 , limit + 1):
23+
sieve[i] = False
24+
25+
"""Mark sieve[n] is True if
26+
one of the following is True:
27+
a) n = (4*x*x)+(y*y) has odd
28+
number of solutions, i.e.,
29+
there exist odd number of
30+
distinct pairs (x, y) that
31+
satisfy the equation and
32+
n % 12 = 1 or n % 12 = 5.
33+
b) n = (3*x*x)+(y*y) has
34+
odd number of solutions
35+
and n % 12 = 7
36+
c) n = (3*x*x)-(y*y) has
37+
odd number of solutions,
38+
x > y and n % 12 = 11 """
39+
x = 1
40+
while(x * x < limit ) :
41+
y = 1
42+
while(y * y < limit ) :
43+
44+
# Main part of
45+
# Sieve of Atkin
46+
n = (4 * x * x) + (y * y)
47+
if (n <= limit and (n % 12 == 1 or
48+
n % 12 == 5)):
49+
sieve[n] ^= True
50+
51+
n = (3 * x * x) + (y * y)
52+
if (n <= limit and n % 12 == 7):
53+
sieve[n] ^= True
54+
55+
n = (3 * x * x) - (y * y)
56+
if (x > y and n <= limit and
57+
n % 12 == 11):
58+
sieve[n] ^= True
59+
y += 1
60+
x += 1
61+
62+
# Mark all multiples of
63+
# squares as non-prime
64+
r = 5
65+
while(r * r < limit) :
66+
if (sieve[r]) :
67+
for i in range(r * r, limit, r * r):
68+
sieve[i] = False
69+
70+
# Print primes
71+
# using sieve[]
72+
for a in range(5 , limit ):
73+
if (sieve[a]):
74+
print(a , end = " ")
75+
76+
# Driver Code
77+
limit = 20
78+
SieveOfAtkin(limit)
79+
80+
# This code is contributed
81+
# by Smitha

general.log

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[2022-06-11 22:44:37 INFO]: install::setup-logging ended
2+
================
3+
[2022-06-11 22:44:37 INFO]: install::start-up started
4+
[2022-06-11 22:44:37 INFO]: install::start-up build image
5+
[2022-06-11 22:44:37 INFO]: install::start-up enabling xhost connection to image
6+
[2022-06-11 22:44:37 INFO]: install::start-up running image
7+
[2022-06-11 22:44:37 INFO]: install::start-up ended
8+
================

install.sh

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env bash
2+
3+
basefile="install"
4+
logfile="general.log"
5+
timestamp=`date '+%Y-%m-%d %H:%M:%S'`
6+
7+
if [ "$#" -ne 1 ]; then
8+
msg="[ERROR]: $basefile failed to receive enough args"
9+
echo "$msg"
10+
echo "$msg" >> $logfile
11+
exit 1
12+
fi
13+
14+
function setup-logging(){
15+
scope="setup-logging"
16+
info_base="[$timestamp INFO]: $basefile::$scope"
17+
18+
echo "$info_base started" >> $logfile
19+
20+
echo "$info_base removing old logs" >> $logfile
21+
22+
rm -f $logfile
23+
24+
echo "$info_base ended" >> $logfile
25+
26+
echo "================" >> $logfile
27+
}
28+
29+
function root-check(){
30+
scope="root-check"
31+
info_base="[$timestamp INFO]: $basefile::$scope"
32+
33+
echo "$info_base started" >> $logfile
34+
35+
#Make sure the script is running as root.
36+
if [ "$UID" -ne "0" ]; then
37+
echo "[$timestamp ERROR]: $basefile::$scope you must be root to run $0" >> $logfile
38+
echo "==================" >> $logfile
39+
echo "You must be root to run $0. Try the following"
40+
echo "sudo $0"
41+
exit 1
42+
fi
43+
44+
echo "$info_base ended" >> $logfile
45+
echo "================" >> $logfile
46+
}
47+
48+
function docker-check() {
49+
scope="docker-check"
50+
info_base="[$timestamp INFO]: $basefile::$scope"
51+
cmd=`docker -v`
52+
53+
echo "$info_base started" >> $logfile
54+
55+
if [ -z "$cmd" ]; then
56+
echo "$info_base docker not installed"
57+
echo "$info_base docker not installed" >> $logfile
58+
fi
59+
60+
echo "$info_base ended" >> $logfile
61+
echo "================" >> $logfile
62+
63+
}
64+
65+
function usage() {
66+
echo ""
67+
echo "Usage: "
68+
echo ""
69+
echo "-u: start."
70+
echo "-d: tear down."
71+
echo "-h: Display this help and exit."
72+
echo ""
73+
}
74+
function start-up(){
75+
76+
scope="start-up"
77+
docker_img_name=`head -n 1 README.md | sed 's/# //'`
78+
info_base="[$timestamp INFO]: $basefile::$scope"
79+
80+
echo "$info_base started" >> $logfile
81+
82+
echo "$info_base build image" >> $logfile
83+
84+
sudo docker build -t $docker_img_name .
85+
86+
echo "$info_base enabling xhost connection to image" >> $logfile
87+
88+
xhost +localhost
89+
90+
echo "$info_base running image" >> $logfile
91+
92+
sudo docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix $docker_img_name
93+
94+
echo "$info_base ended" >> $logfile
95+
96+
echo "================" >> $logfile
97+
}
98+
function tear-down(){
99+
100+
scope="tear-down"
101+
info_base="[$timestamp INFO]: $basefile::$scope"
102+
103+
echo "$info_base started" >> $logfile
104+
105+
echo "$info_base services removed" >> $logfile
106+
107+
echo "$info_base ended" >> $logfile
108+
109+
echo "================" >> $logfile
110+
}
111+
112+
root-check
113+
docker-check
114+
115+
while getopts ":udh" opts; do
116+
case $opts in
117+
u)
118+
setup-logging
119+
start-up ;;
120+
d)
121+
tear-down ;;
122+
h)
123+
usage
124+
exit 0 ;;
125+
/?)
126+
usage
127+
exit 1 ;;
128+
esac
129+
done

0 commit comments

Comments
 (0)