^(?!(..+)(\1)+$)
Why does that work on primes? I got it by mistake when fiddling with the parenthesis locations but I was expecting to have to deal with xx separately. ^(?!(..+)(\1)+$)
Why does that work on primes? I got it by mistake when fiddling with the parenthesis locations but I was expecting to have to deal with xx separately.You've solved it using the actual definition of prime numbers, no trickery needed. Well played.
FYI, you don't need brackets around the \1, so can score 286.
- the first one is represented by the group (..+) it represents the number of occurrences n between 2 and +∞
- the second one is represented by (\1)+. We will repeat the first number m times, between 1 and +∞ times.
So the result of the multiplication is n*(m+1), which cannot be a prime. We just have to take the opposite with negative lookahead. It's very beautiful indeed.
See http://regex101.com/r/qN2fQ8 or http://www.regexper.com/#^%28%3F!%28..%2B%29\1%2B%24%29 to follow the above explanations.
(I learned that for (\1)+, "Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data")
DB<63> print "Matches!" if (("x" x 31) !~ /^(..+)\1+$/)
Matches! DB<64> print "Matches!" if (("x" x 18) !~ /^(..+)\1+$/)
The Regex Golf site only asserts matches, i.e., it's using =~. That's why the negation using negative lookahead was needed.(The simpler regex merely looks for non-primes by matching any number of characters which are a multiple of two numbers, n x m, i.e., those which can be factorized. n comes from (..+), m comes from \1+).