Isolated prime definition from Wikipedia:
An isolated prime is a prime number p such that neither p − 2 nor p + 2 is prime. In other words, p is not part of a twin prime pair. For example, 23 is an isolated prime since 21 and 25 are both composite.
Consider strings which consists of character
x repeated n times, where n ≥ 0. You shall write a regex which matches strings whose length is an isolated prime, and rejects strings whose length is not an isolated prime.
Same rule as my previous question Match strings whose length is a fourth power:
- Any (ir)regular expression syntax is allowed, except for arbitrary code execution (to Perl users).
- Your solution should logically1 work correctly for large number. This means that an answer that lists out all the lengths within certain range is disqualified.
- Please also include a short explanation about your approach to the problem (something like this).
1 Some engine implements
* with an upper limit. This question assumes that
* doesn't have an upper limit (and the only limit is the length of the input string).
This question on SO might be useful as a starting point. How to determine if a number is a prime with regex?
Do you know?
If you can solve this problem, with trivial modification, you can check whether a number is part of a twin prime, since isolated prime has opposite definition of a twin prime.