Published: Jul 22 2010 / 08:02
Uh, ok. Took some time to understand you first have to transform your number into a sequence of ones.
1 > 1 > match (no prime)
2 > 11 > no match (prime)
3 > 111 > no match (prime)
4 > 1111 > match (no prime)
Allow me to question this technique as being practical for checking if let's say 25209506681 is a prime or not.
This isn't a regular expression, really. The \1 refers to the last match, and that requires remembering your matches -- and that's not strictly a regular expression... you need more than a Finite State Machine and it takes more than linear time to evaluate. (see Patterns for non-regular languages in http://en.wikipedia.org/wiki/Regular_expression).
For description of how this works, see http://montreal.pm.org/tech/neil_kandalgaonkar.shtml,
Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.