Created
December 8, 2013 16:42
-
-
Save matthieuprat/7859973 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adapted from <http://neilk.net/blog/2000/06/01/abigails-regex-to-test-for-prime-numbers/>. | |
*/ | |
function isPrime(n) { | |
return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#')); | |
} | |
(function (n){return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'))})(1); // false | |
(function (n){return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'))})(2); // true | |
(function (n){return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'))})(7); // true | |
(function (n){return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'))})(9); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment