-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.js
More file actions
43 lines (40 loc) · 1.14 KB
/
command.js
File metadata and controls
43 lines (40 loc) · 1.14 KB
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
/**
* Executes a CPU-intensive task of calculating prime numbers and calls a callback function when done.
* @param {Function} callback - The callback function to be called when the task is completed.
*/
function cpuIntensiveTask(callback) {
const primeNumbers = calculatePrimeNumbers(30000);
callback(`CPU-intensive task completed. Prime numbers: ${primeNumbers.length}`);
}
/**
* Calculates prime numbers up to a given limit.
* @param {number} limit - The upper limit for the prime numbers calculation.
* @returns {Array} An array of prime numbers up to the given limit.
*/
function calculatePrimeNumbers(limit) {
const primes = [];
for (let i = 2; i <= limit; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
return primes;
}
/**
* Checks if a given number is prime.
* @param {number} num - The number to check.
* @returns {boolean} True if the number is prime, false otherwise.
*/
function isPrime(num) {
for (let i = 2, sqrt = Math.sqrt(num); i <= sqrt; i++) {
if (num % i === 0) {
return false;
}
}
return num > 1;
}
module.exports = {
cpuIntensiveTask,
calculatePrimeNumbers,
isPrime
};