summaryrefslogtreecommitdiff
path: root/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala')
-rw-r--r--src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala38
1 files changed, 10 insertions, 28 deletions
diff --git a/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala b/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala
index 2ad3ba6..ca73e6b 100644
--- a/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala
+++ b/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala
@@ -4,6 +4,7 @@ package com.nsrddyn
class Prime(threads: Int) {
+
/*
* Calculate all primes up to limit
* This should stress the ALU in someway,
@@ -11,36 +12,17 @@ class Prime(threads: Int) {
* will hopefully keep the cpu pipeline busy
* and that way stress the branch predictor
*
- * */
-
-
- // TODO: bad implementation of scala, scala prefers functional programming which something I am not doing here
-
- def run(n: Long): Unit = {
-
-
- var iterator = 0
-
- // TODO: run the isPrime checks
-
- }
+ * math.sqrt(n) => a prime number has 2 factors, one of the factors
+ * of the prime numbers has to be smaller then n
+ * after that we check if the number is whole number and thereby checking if its a prime
+ *
+ */
def isPrime(n: Int): Boolean = {
-
- for
- i <- 2 to 5
- if isWholeNumber(n % i) == true then
- true
-
- false
-
- // TODO: calculate if the number is a prime number
- // TODO: fix errors
+ if n <= 1 then false
+ else !(2 to math.sqrt(n).toInt).exists(i => n % i == 0)
}
- def isWholeNumber(n: Int | Float): Boolean = {
- // TODO: calculate if the number is a whole number
- }
-
-
+ def run(n: Int): Unit = for i <- 0 to n do isPrime(i)
}
+