Skip to content

Instantly share code, notes, and snippets.

@NickBaynham
Created August 18, 2017 21:46
Show Gist options
  • Save NickBaynham/ed9390d556725715bd701fc0249daba6 to your computer and use it in GitHub Desktop.
Save NickBaynham/ed9390d556725715bd701fc0249daba6 to your computer and use it in GitHub Desktop.
Prime with Cache
function isPrime(value) {
if (!isPrime.answers) {
isPrime.answers = {};
}
if (isPrime.answers[value] !== undefined) {
return isPrime.answers[value];
}
var prime = value !== 1;
for (var i = 2; i < value; i++) {
if (value % i === 0) {
prime = false;
break;
}
}
return isPrime.answers[value] = prime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment