HTML5 Canvas
Written by: Simon Sarris
Featured Refcardz: Top Refcardz:
  1. Apache Hadoop
  2. Web Driver
  3. MVVM
  4. REST
  5. ADO.NET
  1. HTML5
  2. Ajax
  3. jQuery Selectors
  4. CSS Part 1
  5. Git

Link Details

Link 451743 thumbnail
User 530255 avatar

By mWo12
via noulakaz.net
Published: Jul 22 2010 / 08:02

Regular expressions are extremely powerful: /^1?$|^(11+?)\1+$/ checks if a number is prime or not.
  • 30
  • 1
  • 11299
  • 3

Comments

Add your comment
User 781709 avatar

mpo replied ago:

0 votes Vote down Vote up Reply

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)
etc etc

Allow me to question this technique as being practical for checking if let's say 25209506681 is a prime or not.

User 0 avatar

Scoundrel replied ago:

0 votes Vote down Vote up Reply

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,

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.