Skip to content

Commit a55965f

Browse files
committed
initial commit
0 parents  commit a55965f

File tree

6 files changed

+287
-0
lines changed

6 files changed

+287
-0
lines changed

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM gradle:jdk11
2+
3+
WORKDIR /app
4+
5+
ADD --chown=gradle:gradle /bin/ .
6+
7+
RUN chmod -R +x *
8+
9+
CMD ["gradle", "run"]

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# java-cli-gradle-sieve-seg
2+
3+
## Description
4+
Find all primes less in a range. Follows functional programming practices.
5+
6+
## Tech stack
7+
- java
8+
- gradle
9+
10+
## Docker stack
11+
- gradle:jdk11
12+
13+
## To run
14+
`sudo ./install.sh -u`
15+
16+
## To stop (optional)
17+
`sudo ./install.sh -d`
18+
19+
## For help
20+
`sudo ./install.sh -h`

bin/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apply plugin: 'java'
2+
apply plugin: 'application'
3+
4+
repositories {
5+
mavenCentral()
6+
}
7+
8+
mainClassName = "example.Main"
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package example;
2+
3+
// Java program to print print all primes smaller than
4+
// n using segmented sieve
5+
6+
import java.util.Vector;
7+
import static java.lang.Math.sqrt;
8+
import static java.lang.Math.floor;
9+
10+
class Main
11+
{
12+
// This methid finds all primes smaller than 'limit'
13+
// using simple sieve of eratosthenes. It also stores
14+
// found primes in vector prime[]
15+
static void simpleSieve(int limit, Vector<Integer> prime)
16+
{
17+
// Create a boolean array "mark[0..n-1]" and initialize
18+
// all entries of it as true. A value in mark[p] will
19+
// finally be false if 'p' is Not a prime, else true.
20+
boolean mark[] = new boolean[limit+1];
21+
22+
for (int i = 0; i < mark.length; i++)
23+
mark[i] = true;
24+
25+
for (int p=2; p*p<limit; p++)
26+
{
27+
// If p is not changed, then it is a prime
28+
if (mark[p] == true)
29+
{
30+
// Update all multiples of p
31+
for (int i=p*p; i<limit; i+=p)
32+
mark[i] = false;
33+
}
34+
}
35+
36+
// Print all prime numbers and store them in prime
37+
for (int p=2; p<limit; p++)
38+
{
39+
if (mark[p] == true)
40+
{
41+
prime.add(p);
42+
System.out.print(p + " ");
43+
}
44+
}
45+
}
46+
47+
// Prints all prime numbers smaller than 'n'
48+
static void segmentedSieve(int n)
49+
{
50+
// Compute all primes smaller than or equal
51+
// to square root of n using simple sieve
52+
int limit = (int) (floor(sqrt(n))+1);
53+
Vector<Integer> prime = new Vector<>();
54+
simpleSieve(limit, prime);
55+
56+
// Divide the range [0..n-1] in different segments
57+
// We have chosen segment size as sqrt(n).
58+
int low = limit;
59+
int high = 2*limit;
60+
61+
// While all segments of range [0..n-1] are not processed,
62+
// process one segment at a time
63+
while (low < n)
64+
{
65+
if (high >= n)
66+
high = n;
67+
68+
// To mark primes in current range. A value in mark[i]
69+
// will finally be false if 'i-low' is Not a prime,
70+
// else true.
71+
boolean mark[] = new boolean[limit+1];
72+
73+
for (int i = 0; i < mark.length; i++)
74+
mark[i] = true;
75+
76+
// Use the found primes by simpleSieve() to find
77+
// primes in current range
78+
for (int i = 0; i < prime.size(); i++)
79+
{
80+
// Find the minimum number in [low..high] that is
81+
// a multiple of prime.get(i) (divisible by prime.get(i))
82+
// For example, if low is 31 and prime.get(i) is 3,
83+
// we start with 33.
84+
int loLim = (int) (floor(low/prime.get(i)) * prime.get(i));
85+
if (loLim < low)
86+
loLim += prime.get(i);
87+
88+
/* Mark multiples of prime.get(i) in [low..high]:
89+
We are marking j - low for j, i.e. each number
90+
in range [low, high] is mapped to [0, high-low]
91+
so if range is [50, 100] marking 50 corresponds
92+
to marking 0, marking 51 corresponds to 1 and
93+
so on. In this way we need to allocate space only
94+
for range */
95+
for (int j=loLim; j<high; j+=prime.get(i))
96+
mark[j-low] = false;
97+
}
98+
99+
// Numbers which are not marked as false are prime
100+
for (int i = low; i<high; i++)
101+
if (mark[i - low] == true)
102+
System.out.print(i + " ");
103+
104+
// Update low and high for next segment
105+
low = low + limit;
106+
high = high + limit;
107+
}
108+
}
109+
110+
// Driver method
111+
public static void main(String args[])
112+
{
113+
int n = 100;
114+
System.out.println("Primes smaller than " + n + ":");
115+
segmentedSieve(n);
116+
}
117+
}

general.log

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[2024-08-27 11:35:18 INFO]: install::setup-logging ended
2+
================
3+
[2024-08-27 11:35:18 INFO]: install::start-up started
4+
[2024-08-27 11:35:18 INFO]: install::start-up build image
5+
[2024-08-27 11:35:18 INFO]: install::start-up running image
6+
[2024-08-27 11:35:18 INFO]: install::start-up ended
7+
================

install.sh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
78+
docker_img_name=`head -n 1 README.md | sed 's/# //'`
79+
info_base="[$timestamp INFO]: $basefile::$scope"
80+
81+
echo "$info_base started" >> $logfile
82+
83+
echo "$info_base build image" >> $logfile
84+
85+
echo "$info_base running image" >> $logfile
86+
87+
sudo docker build -t $docker_img_name .
88+
89+
sudo docker run --rm $docker_img_name
90+
91+
echo "$info_base ended" >> $logfile
92+
93+
echo "================" >> $logfile
94+
}
95+
function tear-down(){
96+
97+
scope="tear-down"
98+
info_base="[$timestamp INFO]: $basefile::$scope"
99+
100+
echo "$info_base started" >> $logfile
101+
102+
echo "$info_base services removed" >> $logfile
103+
104+
echo "$info_base ended" >> $logfile
105+
106+
echo "================" >> $logfile
107+
}
108+
109+
root-check
110+
docker-check
111+
112+
while getopts ":udh" opts; do
113+
case $opts in
114+
u)
115+
setup-logging
116+
start-up ;;
117+
d)
118+
tear-down ;;
119+
h)
120+
usage
121+
exit 0 ;;
122+
/?)
123+
usage
124+
exit 1 ;;
125+
esac
126+
done

0 commit comments

Comments
 (0)