From 94e2465ded436006cdd3f57dc6d4e3f733c28733 Mon Sep 17 00:00:00 2001 From: nasr Date: Mon, 17 Nov 2025 23:57:36 +0100 Subject: [PATCH] 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. --- src/main/scala/com/nsrddyn/Main.scala | 21 ++++++---- .../scala/com/nsrddyn/cpu/ALU/Prime.scala | 38 +++++-------------- 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/main/scala/com/nsrddyn/Main.scala b/src/main/scala/com/nsrddyn/Main.scala index 7d5aa66..3cc63a3 100644 --- a/src/main/scala/com/nsrddyn/Main.scala +++ b/src/main/scala/com/nsrddyn/Main.scala @@ -1,15 +1,20 @@ package com.nsrddyn - object Torque { -import com.nsrddyn + import java.time.Instant - @main def HelloWorld(args: String*): Unit = - println("--- TORQUE STRESS TESTING UTILITY") - val prime = new Prime() + @main def main(args: String*): Unit = - prime.run(3, 5) - + // ANSI ESCAPE CODE: clear screen + println("\u001b[2J\u001b[H") + println("--- TORQUE STRESS TESTING UTILITY ---") + + val now: Instant = Instant.now() + println(now) + + val pr = new Prime(4) + + val intMax = 2147483647 + pr.run(intMax) } - 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) } +