logfile.ch

"Demystifying The Regular Expression That Checks If A Number Is Prime"

I love this approach to check if a number is a prime using regular expressions. Everything marrying real math with the strange world that are regular expressions is awesome by definition. That’s the Java code:

public static boolean isPrime(int n) {
  return !new String(new char[n]).matches(".?|(..+?)\\1+");
}

Don’t worry. The link above explains everything.