From 94e2465ded436006cdd3f57dc6d4e3f733c28733 Mon Sep 17 00:00:00 2001 From: nasr Date: Mon, 17 Nov 2025 23:57:36 +0100 Subject: 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/cpu/ALU/Prime.scala | 38 +++++++------------------- 1 file changed, 10 insertions(+), 28 deletions(-) (limited to 'src/main/scala/com/nsrddyn/cpu') 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) } + -- cgit v1.2.3-70-g09d2