this question has answer here:
- how regular expression work? 2 answers
i found nice piece of regex code checks prime number. think understand i'm still little confused. here's code: /^1?$|^(11+?)\1+$/
can explain (step step) happening both regex code , how relates knowing if number prime or not?
the basic premise regular expression examines ones representation of number (e.g. 5 = 11111). checking presence of ones (1
) in positions or groupings can identify number prime.
additional references:
- credit credit due - http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
- great explanation - http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/
Comments
Post a Comment