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

Coding for Kids

What is coding? In coding we build a program to do a specific task for us. Code: A code is a set of computer instructions and when you will ...