Problem Description
A prime number is a natural number that has no divisors other than 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers. In this problem, you need to find curious primes. Curious primes refer to primes determined by certain criteria.
Problem Definition
For a given integer N
, return an array containing all the curious primes that are smaller than N
. Curious primes are defined by the following criteria:
- A curious prime must be a natural number greater than or equal to 2.
- If the sum of the digits of the prime exceeds 10, it is not a curious prime.
- The number of odd digits must be greater than the number of even digits.
- Each digit must be an integer and cannot include negative numbers.
Input and Output Format
Input: A positive integer N
(2 ≤ N
≤ 10,000)
Output: An array of integers (curious primes)
Solution Process
Step 1: Implement Prime Checking Function
Implement a function that checks if a number is prime. This function should verify whether the input number is prime by checking for factors from 2 up to the square root of the number.
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}
Step 2: Implement Curious Prime Checking Function
Implement a function to check for curious primes. This function should include functionalities to calculate the sum of the digits and count the digits.
function isCuriousPrime(num) {
if (!isPrime(num)) return false;
const digits = num.toString().split('').map(Number);
const sumOfDigits = digits.reduce((acc, digit) => acc + digit, 0);
const oddCount = digits.filter(digit => digit % 2 !== 0).length;
const evenCount = digits.filter(digit => digit % 2 === 0).length;
return sumOfDigits <= 10 && oddCount > evenCount;
}
Step 3: Implement Result Generation Function
Now, create the main function findCuriousPrimes
that will find all curious primes smaller than the given N
.
function findCuriousPrimes(N) {
const curiousPrimes = [];
for (let i = 2; i < N; i++) {
if (isCuriousPrime(i)) {
curiousPrimes.push(i);
}
}
return curiousPrimes;
}
Step 4: Complete Code and Example Execution
Combine the functions created above to complete the full code. Below is the final code example.
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}
function isCuriousPrime(num) {
if (!isPrime(num)) return false;
const digits = num.toString().split('').map(Number);
const sumOfDigits = digits.reduce((acc, digit) => acc + digit, 0);
const oddCount = digits.filter(digit => digit % 2 !== 0).length;
const evenCount = digits.filter(digit => digit % 2 === 0).length;
return sumOfDigits <= 10 && oddCount > evenCount;
}
function findCuriousPrimes(N) {
const curiousPrimes = [];
for (let i = 2; i < N; i++) {
if (isCuriousPrime(i)) {
curiousPrimes.push(i);
}
}
return curiousPrimes;
}
console.log(findCuriousPrimes(50)); // Example Output: [3, 5, 7, 11, 13, 17, 23, 29, 31, 37, 41, 43, 47]
Optimization Plan
The current algorithm has a time complexity proportional to N, and there is room for improvement. You can optimize by calculating primes in advance and storing them in an array, then using this array to find curious primes.
Conclusion
This article discussed methods for finding curious primes in JavaScript. The algorithm was explained step by step, and the final code was included. By solving this problem, one can deepen their understanding of conditional statements, loops, and array manipulation in JavaScript.