Skip to content

Commit

Permalink
Groovy solution by mmcdon20 (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcdon20 authored Aug 5, 2021
1 parent b8142b8 commit c4734f8
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
4 changes: 4 additions & 0 deletions PrimeGroovy/solution_1/Dockerfile
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" ]
106 changes: 106 additions & 0 deletions PrimeGroovy/solution_1/PrimeGroovy.groovy
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
}
}
}
}
29 changes: 29 additions & 0 deletions PrimeGroovy/solution_1/README.md
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
```

0 comments on commit c4734f8

Please sign in to comment.