Skip to content

Instantly share code, notes, and snippets.

@ganeshkbhat
Last active November 25, 2022 08:07
Show Gist options
  • Save ganeshkbhat/9af2ea875cdcc01888cd50a4f962f655 to your computer and use it in GitHub Desktop.
Save ganeshkbhat/9af2ea875cdcc01888cd50a4f962f655 to your computer and use it in GitHub Desktop.
Calculate, work with primes or check primes in a very fast way possible: fast-prime.js
//
// SPECIAL LICENSE - PROPRIETARY LICENSE AGREEMENT ONLY
//
// https://github.com/ganeshkbhat/fastprimenumbers
//
/*
PROPRIETARY LICENSE AGREEMENT ONLY
Use for your opensource projects only. If you use it or modify it for your project please spend 2 minutes of your time crediting me
by letting me know by either sending me an email, or logging an issue with subject heading `[User]`, or `star` my project. But
let me know. I would love to know the fans and users of the code logic
*/
function prime(n) {
if ((n === 2 || n === 3 || n === 5 || n === 7)) {
return true;
}
if (n === 1 || ((n > 7) && (n % 5 == 0 || n % 7 == 0 || n % 2 == 0 || n % 3 == 0))) {
return false;
}
if ((((n - 1) % 6) === 0) || (((n + 1) % 6) === 0)) {
for (let i = 1; i < n; i++) {
let factorsix = (i * 6), fivebase = (5 + factorsix), sevenbase = (7 + factorsix);
if (((n > fivebase) && ((n % fivebase) === 0)) || ((n > sevenbase) && ((n % sevenbase) === 0))) {
return false;
}
if (factorsix > n) {
break;
}
}
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment