JavaScript prototype to find two primes whose sum is the given number

This program finds two prime numbers whose sum is the number provided by the user.

Took 8 minutes and 3 seconds to code JavaScript prototype to find two primes whose sum is the given number. Here is the code snippet.


Code (Run Code):

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeSum(num) {
arr = [];
for (var i = 2; i < num; i++) {
if (checkPrime(i)) {
arr.push(i);
}
}
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] == num) {
return arr[i] + ' + ' + arr[j] + ' = ' + num;
}
}
}
}

function main(){
document.getElementById("result").innerHTML =
primeSum(document.getElementById("num").value);
}

1 comment:

  1. Top Restaurants near Foxwoods Casino, Connecticut | MapYRO
    ‎Casino · 충주 출장마사지 ‎Horse Racing · ‎Wolf Den · 경기도 출장샵 ‎Chile Casino · 김포 출장샵 ‎Poker 서산 출장안마 tables · 제천 출장안마 ‎Free · ‎Shopping

    ReplyDelete

GITEX Dubai Talk by Saion Gupta and Saiasmi Gupta

Good morning, everyone. I'm Saion Gupta, the Founder of 10xCoderKids and the Guinness World Record holder for the youngest computer prog...