Javascript 266
function N(a){function b(a){return P.every(function(b){if(n=b,i=a.length,j=b.length,j>i) return;if(j==i) return 1;while(n.length<i)n+=b;return n.length!=i})}if(q=A,A!=a)for(;q.length.toString()!=a;)b(q)&&P.push(q),q+=A;console.log(b(q)?"Prime":"Not!!")}A="0",P=[A+A]
Menciptakan fungsi yang disebut N yang akan mencetak hasil yang diinginkan. Versi yang tidak ditambang terlihat seperti ini. Saya melakukan minify tangan untuk membersihkan beberapa variabel dan kemudian menjalankannya melalui uglify dan kemudian tangan minify itu lagi.
// A a string of "0" for using to generate long strings
// P is the store for all known primes
A="0", P=[A+A];
function N(val) {
function _isPrime(str) {
// go through all the known primes and return true
// if we don't match on any of them
return P.every(function(prime) {
// prime is some known string whose length is a prime number
tsr = prime, strlen = str.length, primelen = prime.length;
// if the string we're checking has fewer chars than
// this then it's not a prime
if(strlen < primelen) return 0;
// if the string we're checking has the same number of chars
// as the the prime we're checking against then it is a prime
if(primelen == strlen) return 1;
// Keep incrementing our temporary string with the prime we're
// checking. we'll break out of the loop once the temporary string
// is greater than or equal to the string we're testing
while(tsr.length < strlen) {
tsr += prime;
}
return !(tsr.length == strlen)
});
}
// start with a string of one unit
nstr = A
if(A!=val) {
// keep incrementing the string so that we can compile a list
// of known primes smaller than this value
while(nstr.length.toString() !== val) {
if(_isPrime(nstr)) {
P.push(nstr);
}
nstr += A;
}
}
console.log(_isPrime(nstr) ? "Prime" : "Not!!");
}
Mengujinya menggunakan cuplikan ini:
for(var X=0;X<10;X++) {
console.log('checking: ' + X);
N(X.toString());
}