Determine if a number is a prime with regex?

Posted by kitlite on Stack Overflow See other posts from Stack Overflow or by kitlite
Published on 2010-05-08T17:58:34Z Indexed on 2010/05/08 18:08 UTC
Read the original article Hit count: 138

Filed under:
|

I found the following code example for Java on RosettaCode:

public static boolean prime(int n) {
return !new String(new char[n]).matches(".?|(..+?)\\1+");
}
  • I don't know Java in particular but understand all aspects of this snippet except for the regex itself
  • I have basic to basic-advanced knowledge of Regex as you find it in the built-in PHP functions

How does .?|(..+?)\\1+ match prime numbers?

© Stack Overflow or respective owner

Related posts about java

Related posts about regex