summaryrefslogtreecommitdiff
path: root/src/main/scala/com/nsrddyn/cpu/ALU
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2025-11-17 23:57:36 +0100
committernasr <nsrddyn@gmail.com>2025-11-17 23:57:36 +0100
commit94e2465ded436006cdd3f57dc6d4e3f733c28733 (patch)
tree9e179b1fd3be18e1f72b0302f874ece975b16198 /src/main/scala/com/nsrddyn/cpu/ALU
parenta66ec9fb875ece99c35cbe8c63d13228043d3d84 (diff)
feature(updatefeature(update): prime calculation method
finished the prime number calculation method, added an example of it calculating the max int number. stresses cpu usage to 100%, not heavy yet. But we are stressing it in someway. next steps are to add multithreading and different ways of performance measurments.
Diffstat (limited to 'src/main/scala/com/nsrddyn/cpu/ALU')
-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)
}
+