Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Summing up digits and finding nearest prime in JavaScript
We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.
This problem involves two main steps: calculating the digit sum and finding the nearest prime number greater than or equal to that sum.
Breaking Down the Solution
Our solution requires three functions:
-
digitSum()- calculates the sum of all digits in a number -
isPrime()- checks if a number is prime -
nearestPrime()- finds the nearest prime >= digit sum
Example Implementation
const num = 56563;
const digitSum = (num, sum = 0) => {
if(num){
return digitSum(Math.floor(num / 10), sum + (num % 10));
}
return sum;
};
const isPrime = n => {
if (n === 1){
return false;
} else if(n === 2){
return true;
} else {
for(let x = 2; x < n; x++){
if(n % x === 0){
return false;
}
}
return true;
}
};
const nearestPrime = num => {
let sum = digitSum(num);
console.log(`Digit sum of ${num}: ${sum}`);
while(!isPrime(sum)){
sum++;
}
return sum;
};
console.log(`Nearest prime: ${nearestPrime(num)}`);
Digit sum of 56563: 25 Nearest prime: 29
How It Works
Let's trace through the execution with number 56563:
- Digit Sum: 5 + 6 + 5 + 6 + 3 = 25
- Prime Check: 25 is not prime (divisible by 5)
- Next Numbers: 26 (not prime), 27 (not prime), 28 (not prime)
- Found: 29 is prime
Testing with Different Numbers
// Test with different numbers
const testNumbers = [123, 999, 17];
testNumbers.forEach(num => {
const sum = digitSum(num);
const prime = nearestPrime(num);
console.log(`${num} -> digit sum: ${sum} -> nearest prime: ${prime}`);
});
123 -> digit sum: 6 -> nearest prime: 7 999 -> digit sum: 27 -> nearest prime: 29 17 -> digit sum: 8 -> nearest prime: 11
Alternative Iterative Approach
// Non-recursive version of digitSum
const digitSumIterative = (num) => {
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
return sum;
};
// Test both approaches
const testNum = 12345;
console.log("Recursive:", digitSum(testNum));
console.log("Iterative:", digitSumIterative(testNum));
Recursive: 15 Iterative: 15
Optimization for Prime Checking
// More efficient prime checking (only check up to sqrt(n))
const isPrimeOptimized = n => {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) {
return false;
}
}
return true;
};
// Compare performance for larger numbers
const largeSum = 97;
console.log(`${largeSum} is prime (optimized):`, isPrimeOptimized(largeSum));
97 is prime (optimized): true
Conclusion
This solution efficiently combines digit summation with prime number detection. The recursive approach for digit sum is elegant, while the prime checking can be optimized for better performance with larger numbers.
Advertisements
