http://www.perlmonks.org?node_id=969141


in reply to is it prime?

Where is my hammer?

sub isPrime($) { $n=pop; 1^(1x$n--)=~/^(1{2,$n})\1+$/; }

With n>2

Just for fun as a contrib to https://github.com/fxn/math-with-regexps/blob/master/one-liners.sh (fxn).

Update, thanks to ambrus's reply: Re^2: is it prime?

sub isPrime($) { (1x pop)!~/^(11+)\1+$/ }