-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM groovy:4.0-jre16 | ||
WORKDIR /opt/app | ||
COPY PrimeGroovy.groovy . | ||
ENTRYPOINT [ "groovy", "PrimeGroovy.groovy" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import java.util.BitSet | ||
|
||
class PrimeGroovy { | ||
private int sieveSize | ||
private BitSet bits | ||
private static def resultsDictionary = [ | ||
10: 4, | ||
100: 25, | ||
1000: 168, | ||
10000: 1229, | ||
100000: 9592, | ||
1000000: 78498, | ||
10000000: 664579, | ||
100000000: 5761455, | ||
1000000000: 50847534, | ||
10000000000: 455052511 | ||
] | ||
|
||
PrimeGroovy(int size) { | ||
this.sieveSize = size | ||
this.bits = new BitSet((size + 1) >> 1) | ||
bits.flip(0, (sieveSize + 1) >> 1) | ||
} | ||
|
||
private boolean validateResults() { | ||
return resultsDictionary[sieveSize] == countPrimes() | ||
} | ||
|
||
private boolean getBit(int index) { | ||
return bits.get(index >> 1) | ||
} | ||
|
||
private void clearBit(int index) { | ||
bits.clear(index >> 1) | ||
} | ||
|
||
void runSieve() { | ||
int factor = 3 | ||
int q = (int) Math.sqrt((double) sieveSize) | ||
|
||
while (factor <= q) { | ||
for (var num = factor; num < sieveSize; num += 2) { | ||
if (getBit(num)) { | ||
factor = num | ||
break | ||
} | ||
} | ||
|
||
for (var num = factor * factor; num < sieveSize; num += factor * 2) { | ||
clearBit(num) | ||
} | ||
|
||
factor += 2 | ||
} | ||
} | ||
|
||
void printResults(boolean showResults, double duration, int passes) { | ||
if (showResults) { | ||
printf("2, ") | ||
} | ||
|
||
int count = (sieveSize >= 2) ? 1 : 0 | ||
|
||
for (var num = 3; num <= sieveSize; num += 2) { | ||
if (getBit(num)) { | ||
if (showResults) { | ||
printf("%d, ", num) | ||
} | ||
count++ | ||
} | ||
} | ||
|
||
if (showResults) { | ||
printf("\n") | ||
printf("Passes: %d, Time: %f, ", passes, duration) | ||
printf("Avg: %f, Limit: %d, ", duration / passes, sieveSize) | ||
printf("Count1: %d, Count2: %d, ", count, countPrimes()) | ||
printf("Valid: %s\n", validateResults() ? "True" : "False") | ||
printf("\n") | ||
} | ||
|
||
printf("mmcdon20_groovy;%d;%f;1;algorithm=base,faithful=yes,bits=1\n", | ||
passes, duration) | ||
} | ||
|
||
int countPrimes() { | ||
return this.bits.cardinality() | ||
} | ||
|
||
static void main(String... args) { | ||
int passes = 0 | ||
long start = System.currentTimeMillis() | ||
|
||
while (true) { | ||
PrimeGroovy sieve = new PrimeGroovy(1000000) | ||
sieve.runSieve() | ||
passes++ | ||
long stop = System.currentTimeMillis() | ||
|
||
if (stop - start >= 5000) { | ||
sieve.printResults(false, (stop - start) / 1000.0, passes) | ||
break | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Groovy solution by mmcdon20 | ||
|
||
![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-1-green) | ||
|
||
A faithful implementation of the prime sieve algorithm in the [groovy language](https://groovy-lang.org/). | ||
|
||
## Run instructions | ||
|
||
To run the solution using Docker, run the following command: | ||
|
||
``` | ||
docker build -t primes-groovy . | ||
docker run --rm -it primes-groovy | ||
``` | ||
|
||
To run the solution using groovy, run the following command: | ||
|
||
``` | ||
groovy PrimeGroovy.groovy | ||
``` | ||
|
||
### Output | ||
|
||
``` | ||
mmcdon20_groovy;213;5.003000;1;algorithm=base,faithful=yes,bits=1 | ||
``` |