-
Notifications
You must be signed in to change notification settings - Fork 255
/
primes.scala
157 lines (139 loc) · 3.38 KB
/
primes.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import scala.collection.mutable.{BitSet, HashMap, Queue, ArrayBuffer}
object Primes {
val UpperBound = 5000000
val Prefix = 32338
class Node(
var children: HashMap[Char, Node] = HashMap(),
var terminal: Boolean = false
)
class Sieve(val limit: Int) {
private val prime = new BitSet(limit + 1)
def toList(): Iterable[Int] = {
var result = ArrayBuffer(2, 3)
for (p <- 5 to limit) {
if (prime(p)) {
result += p
}
}
result
}
def omitSquares(): Sieve = {
var r = 5
while (r * r < limit) {
if (prime(r)) {
var i = r * r
while (i < limit) {
prime(i) = false
i += r * r
}
}
r += 1
}
this
}
def step1(x: Int, y: Int): Unit = {
val n = (4 * x * x) + (y * y)
if (n <= limit && (n % 12 == 1 || n % 12 == 5)) {
prime(n) = !prime(n)
}
}
def step2(x: Int, y: Int): Unit = {
val n = (3 * x * x) + (y * y)
if (n <= limit && n % 12 == 7) {
prime(n) = !prime(n)
}
}
def step3(x: Int, y: Int): Unit = {
val n = (3 * x * x) - (y * y)
if (x > y && n <= limit && n % 12 == 11) {
prime(n) = !prime(n)
}
}
def loopY(x: Int): Unit = {
var y = 1
while (y * y < limit) {
step1(x, y)
step2(x, y)
step3(x, y)
y += 1
}
}
def loopX(): Unit = {
var x = 1
while (x * x < limit) {
loopY(x)
x += 1
}
}
def calc(): Sieve = {
loopX()
omitSquares()
}
}
def generateTrie(l: Iterable[Int]): Node = {
var root = new Node()
for (el <- l) {
var head = root
for (ch <- el.toString) {
head = head.children.getOrElseUpdate(ch, new Node())
}
head.terminal = true
}
root
}
def find(upperBound: Int, prefix: Int): Option[Iterable[Int]] = {
val primes = new Sieve(upperBound).calc()
val strPrefix = prefix.toString
var head: Option[Node] = Some(generateTrie(primes.toList()))
for (ch <- strPrefix) {
head = head.flatMap(_.children.get(ch))
}
head match {
case Some(head) => {
var queue: Queue[(Node, String)] = Queue((head, strPrefix))
var result: ArrayBuffer[Int] = ArrayBuffer()
while (!queue.isEmpty) {
val (top, prefix) = queue.dequeue()
if (top.terminal) {
result += prefix.toInt
}
for ((ch, v) <- top.children) {
queue += ((v, prefix + ch))
}
}
scala.util.Sorting.stableSort(result)
Some(result)
}
case None => None
}
}
def notify(msg: String): Unit = {
scala.util.Using(
(new java.net.Socket("localhost", 9001)).getOutputStream()
) {
_.write(msg.getBytes())
}
}
def verify(): Unit = {
val left = Seq(2, 23, 29)
find(100, 2) match {
case Some(right) => {
if (left != right) {
System.err.println(s"${left} != ${right}")
System.exit(1)
}
}
case None => {
System.err.println("find() didn't return anything")
System.exit(1)
}
}
}
def main(args: Array[String]): Unit = {
verify()
notify(s"Scala\t${ProcessHandle.current().pid()}")
val results = find(UpperBound, Prefix)
notify("stop")
println(results)
}
}