Open In App

JavaScript Program to check Whether a Number can be Express as Sum of Two Prime Numbers

Last Updated : 28 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To check if a number can be expressed as the sum of two prime numbers, iterate through primes less than or equal to half of the given number. For each prime, check if the complementary prime exists by subtracting it from the target number and testing if the result is prime. If found, the number can be expressed as the sum of two primes.

There are several methods in JavaScript using which one can check whether a number can be expressed as a sum of two prime numbers which are as follows:

Using for loop

In this approach, we are using a for loop to find out the sum. we are checking if the given number is 2 or less than 2 then there is no chance that it will give us the pair of prime numbers. Because 2 can be only the sum of 1 and 1 but 1 is not a prime number. So we check if the given number is greater than 2 for that we will use a for loop to find the prime number and we will sum those prime numbers. By adding those prime numbers we will get the given number which will either return true or false.

Example: In this example The `isPrime` function is used to checks if a number is prime. `SumOfTwoPrimes` checks if a prime number can be the sum of two other primes.

Javascript




function isPrime(n) {
    if (n <= 1) return false;
    for (let i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0) return false;
    }
    return true;
}
 
function SumOfTwoPrimes(N) {
    for (let i = 2; i <= N / 2; i++) {
        if (isPrime(N) && isPrime(N - i)) {
            return true;
        }
    }
    return false;
}
 
const testCase1 = 7;
console.log(SumOfTwoPrimes(testCase1));
 
const testCase2 = 19;
console.log(SumOfTwoPrimes(testCase2));


Output

true
true

Using Two Pointer Approach

The Two Pointer Approach involves using two pointers, one starting from the smallest prime (2) and the other from the given number. While incrementing the first pointer and decrementing the second, it checks if their sum equals the target number.

Example: In this example The code generates prime numbers using the Sieve of Eratosthenes. `isPossible` function checks if a prime can be expressed as the sum of two other primes using a two-pointer approach.

Javascript




function generatePrimes(n) {
    let isPrime = new Array(n + 1).fill(true);
    let primes = [];
 
    for (let i = 2; i <= n; i++) {
        if (isPrime[i]) {
            primes.push(i);
            for (let j = i * i; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
 
    return primes;
}
 
function isPossible(n) {
    let primes = generatePrimes(n);
    let left = 0;
    let right = primes.length - 1;
 
    // Two-pointer approach
    while (left <= right) {
        let sum = primes[left] + primes[right];
        if (sum === n) {
            return true;
        } else if (sum < n) {
            left++;
        } else {
            right--;
        }
    }
 
    return false;
}
 
const testCase1 = 7;
console.log(isPossible(testCase1));
 
const testCase2 = 19;
console.log(isPossible(testCase2));


Output

true
true


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads