News for hackers. Stuff that matters.
Re:A regular expression to check for prime numbers by silvajoao on Sunday July 04, 2010 @06:50PM (#1486580)
The pattern detects if a number N is not prime by searching for A and B such that N = A*B. The (11+?) searches for A bigger than 1, and the repetition of this group (the \1+ part) repeats A as much as possible, eventually B times. If an exact match isn't possible, it backtracks and advances A. If no such A and B exist, A will keep increasing until it becomes bigger than N, and then the match fails.

An interesting pattern that wasn't immediately obvious, but a very inefficient way to detect primes :)

There are no comments on this comment yet.