Skip to content

Instantly share code, notes, and snippets.

@Reelix
Created December 18, 2016 14:22
Show Gist options
  • Save Reelix/c2fcf929c405bd82af8462fadf7efc33 to your computer and use it in GitHub Desktop.
Save Reelix/c2fcf929c405bd82af8462fadf7efc33 to your computer and use it in GitHub Desktop.
JavaScript code to get the sum of the first X prime numbers
function isPrime(value){for(var i=2;i<value;i++){if(value%i===0){return false;}}return value>1;}
function sumOfFirstXPrimeNumbers(count){var currNum=1;var total=0;for(var j=0;j<count;j++){while(true){if(isPrime(currNum)){total+=currNum;currNum++;break;}else{currNum++;}}}return total;}
console.log(sumOfFirstXPrimeNumbers(7));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment