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(''));
}