3 ms·
nice trick! here a JS implementation function isPrime(n) { var re = /^1?$|^(11+?)\1+$/; var s = []; while (n -- > 0) s.push(1);
by _lce0 12y ago
nice trick!
here a JS implementation
function isPrime(n) {
var re = /^1?$|^(11+?)\1+$/;
var s = [];
while (n -- > 0)
s.push(1);
return !re.test(s.join(''));
}
- maemilius 12y agoIf I may suggest an improvement: function isPrime(n) { var re = /^1?$|^(11+?)\1+$/, s = (new Array(n+1)).join('1'); return !re.test(s); }
- _lce0 12y agoman that's genius!!
- quadratini 12y agoes6: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- myfonj 12y agoWhy not just function isPrime(n) { return !/^1?$|^(11+?)\1+$/.test(Array(n+1).join('1')) } then? :]