Skip to content

Commit cf31bb9

Browse files
authored
Red solution by mmcdon20
1 parent 9156bd3 commit cf31bb9

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

PrimeRed/solution_1/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.exe
2+
lib*

PrimeRed/solution_1/Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM kskarthik/redlang:0.6.4
2+
WORKDIR /opt/app
3+
COPY PrimeRed.red .
4+
RUN /usr/local/bin/red -c -r PrimeRed.red
5+
ENTRYPOINT [ "./PrimeRed" ]

PrimeRed/solution_1/PrimeRed.red

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Red [
2+
Author: "mmcdon20"
3+
Purpose: "A Red implementation of David Plummer's PrimeSieve application."
4+
]
5+
6+
PrimeSieve: func [size [integer!] return: [object!]] [
7+
make object! [
8+
sieveSize: size
9+
bits: complement make bitset! ((size + 1) >> 1)
10+
11+
resultsDictionary: #(
12+
10 4
13+
100 25
14+
1000 168
15+
10000 1229
16+
100000 9592
17+
1000000 78498
18+
10000000 664579
19+
100000000 5761455
20+
1000000000 50847534
21+
10000000000 455052511
22+
)
23+
24+
validateResults: func [return: [logic!]] [
25+
resultsDictionary/:sieveSize = countPrimes
26+
]
27+
28+
getBit: func [index [integer!] return: [logic!]] [
29+
bits/(:index >> 1)
30+
]
31+
32+
clearBit: func [index [integer!] return: [logic!]] [
33+
bits/(:index >> 1): false
34+
]
35+
36+
runSieve: func [return: [none!]] [
37+
factor: 3
38+
q: round/down (sqrt sieveSize)
39+
40+
while [factor <= q] [
41+
num: factor
42+
while [num < sieveSize] [
43+
if getBit num [
44+
factor: num
45+
break
46+
]
47+
num: num + 2
48+
]
49+
50+
num: factor * factor
51+
while [num < sieveSize] [
52+
clearBit num
53+
num: num + (factor * 2)
54+
]
55+
56+
factor: factor + 2
57+
]
58+
]
59+
60+
printResults: func [
61+
showResults [logic!]
62+
duration [float!]
63+
passes [integer!]
64+
return: [none!]
65+
] [
66+
if showResults [
67+
write-stdout "2, "
68+
]
69+
70+
count: if sieveSize >= 2 [ 1 ][ 0 ]
71+
72+
num: 3
73+
while [num < sieveSize] [
74+
if getBit num [
75+
if showResults [
76+
write-stdout rejoin [num ", "]
77+
]
78+
count: count + 1
79+
]
80+
num: num + 2
81+
]
82+
83+
if showResults [
84+
write-stdout "^/"
85+
write-stdout rejoin ["Passes: " passes ", Time: " duration ", "]
86+
write-stdout rejoin ["Avg: " (duration / passes) ", Limit: " sieveSize ", "]
87+
write-stdout rejoin ["Count1: " count ", Count2: " countPrimes ", "]
88+
write-stdout rejoin ["Valid: " validateResults "^/"]
89+
]
90+
91+
write-stdout rejoin ["mmcdon20_red;" passes ";" duration ";1;algorithm=base,faithful=yes,bits=1^/"]
92+
]
93+
94+
countPrimes: func [return: [integer!]] [
95+
count: if sieveSize >= 2 [ 1 ][ 0 ]
96+
i: 3
97+
while [i < sieveSize] [
98+
if getBit i [
99+
count: count + 1
100+
]
101+
i: i + 2
102+
]
103+
count
104+
]
105+
]
106+
]
107+
108+
passes: 0
109+
start: to float! now/precise/time
110+
111+
forever [
112+
sieve: PrimeSieve 1000000
113+
do [sieve/runSieve]
114+
passes: passes + 1
115+
stop: to float! now/precise/time
116+
117+
if (stop - start) >= 5.0 [
118+
do [sieve/printResults false (stop - start) passes]
119+
break
120+
]
121+
]

PrimeRed/solution_1/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Red solution by mmcdon20
2+
3+
![Algorithm](https://img.shields.io/badge/Algorithm-base-green)
4+
![Faithfulness](https://img.shields.io/badge/Faithful-yes-green)
5+
![Parallelism](https://img.shields.io/badge/Parallel-no-green)
6+
![Bit count](https://img.shields.io/badge/Bits-1-green)
7+
8+
A faithful implementation of the prime sieve algorithm in the [red programming language](https://www.red-lang.org).
9+
10+
## Run instructions
11+
12+
To run the solution using Docker, run the following command:
13+
14+
```
15+
docker build -t primes-red .
16+
docker run --rm -it primes-red
17+
```
18+
19+
To run the solution using the red cli, run the following command:
20+
21+
```
22+
./red --cli PrimeRed.red
23+
```
24+
25+
### Output
26+
27+
```
28+
mmcdon20_red;5;5.021970000001602;1;algorithm=base,faithful=yes,bits=1
29+
```

0 commit comments

Comments
 (0)