October 31st, 2024

Demystifying the regular expression that checks if a number is prime (2016)

The article presents a regex method in Java for determining prime numbers, explaining its components and providing examples across programming languages to aid understanding for readers of all skill levels.

Read original articleLink Icon
CuriositySkepticismAppreciation
Demystifying the regular expression that checks if a number is prime (2016)

The article discusses a unique method for determining if a number is prime using a regular expression in Java. The code snippet provided utilizes the regex `!new String(new char[n]).matches(".?|(..+?)\\1+")`, which checks the unary representation of a number. The author explains that prime numbers are natural numbers greater than 1 that are only divisible by 1 and themselves. The regex works by matching patterns that indicate non-primality, specifically looking for repetitions of characters that would signify divisibility. The article aims to break down the regex components for readers of all skill levels, providing explanations of regex syntax, capturing groups, backreferences, and greedy versus non-greedy quantifiers. The author emphasizes the importance of understanding how the regex operates, particularly the two parts of the regex: `^.?$` (which matches 0 or 1 character) and `^(..+?)\1+$` (which matches multiples of 2). The explanation is intended to be accessible, even for those unfamiliar with regular expressions, and includes examples in various programming languages to illustrate the concept.

- The article explains a regex method for checking if a number is prime.

- It breaks down the regex components for clarity, targeting readers of all skill levels.

- Prime numbers are defined as natural numbers greater than 1 that are only divisible by 1 and themselves.

- The regex consists of two parts: one for matching 0 or 1 character and another for matching multiples of 2.

- The author provides examples in multiple programming languages to enhance understanding.

AI: What people are saying
The comments on the article about using regex to determine prime numbers reveal a mix of opinions and insights.
  • Several commenters reference the historical context of the regex method, attributing it to Abigail's work in Perl.
  • There is a debate about the efficiency and validity of using regex for primality testing, with some calling it inefficient and not truly a regular expression.
  • Some users express skepticism about the method's sophistication, suggesting it is more of a brute-force approach rather than a clever mathematical trick.
  • Others provide alternative regex examples and resources, highlighting the creativity involved in using regex for mathematical problems.
  • Comments also discuss the specific regex pattern used, clarifying its function in determining whether a unary number is prime.
Link Icon 12 comments
By @aquir - 3 months
Also check the Matt Parker video for a more entertaining explanation: https://www.youtube.com/watch?v=5vbk0TwkokM
By @isoprophlex - 3 months
The precondition that you need to first convert to a unary number makes this feel like it's almost cheating.

The regex is not totally trivial, but it's not super sophisticated either: conceptually 'just' a Sieve of Eratosthenes.

By @IgorPartola - 3 months
So in summary there is no special thing here about this being a regex: the program described by it basically just brute force tries to divide the given number by every number smaller than it, it’s just written in a way that isn’t obvious to understand.

That’s not to detract from the excellent post, just that this isn’t a mathematical trick that exploits some structure of primes but rather an incredibly clever way to write a computer program.

By @pxeger1 - 3 months
I like this regex, which divides a number by sqrt(2):

    (?=(x(x*)).*(?=\1*$)\2+$)(?=(x\1)+(x?(x*)))(?=\4(x(x*?))\1+$)(?=.*(?=(?=\4*$)\4\5+$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\9))(?=.*(?=(?=\4*$)(?=\6*$)(?=\4\7+$)\6\5+$|$\4)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\13))(?=.*(?=\12\12\9$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\17))(?*.*?(?=((?=\3*(x?(x*)))\21(x(x*?))\1+$)))(?=.*(?=\23*$)(\23\24+$))(?=.*(?=(?=\21*$)\21\22+$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\27))(?=.*(?=(?=\21*$)(?=\23*$)(?=\21\24+$)\23\22+$|$\21)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\31))(?=.*(?=\30\30\27$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\35))(?=.*(?=\26\26)(?=\3*(x*))(\1(x)|))(?=.*(?=\34\34\40)(?=\3*(x*))(\1(x)|))(?=(?=(.*)\13\13\17(?=\6*$)\6\7+$)\44(x+|(?=.*(?!\16)\41|(?!.*(?!\38)\8).*(?=\16$)\41$))(\25\31\31\35){2}\43$)\20|xx?\B|
Source: https://codegolf.stackexchange.com/a/198428
By @LunicLynx - 3 months
The title should be: … the regex that checks if the length of a string with the same characters is a prime number.
By @devit - 3 months
That's not a regular expression and it's a ridiculously inefficient way to check for primality.
By @bawolff - 3 months
To save a click, the regex in question is: ^1?$|^(11+?)\1+$ (it checks if a unary number is not prime.

It is kind of surprising to hear that regex can do that, but once you see the regex its kind of obvious. Its just checking if the number is 1, or if the number can be represented by 2 or more 1's repeated at least 2 times. Which is literally the definition of a prime (is the number divisible by a number >= 2)

By @astrodust - 3 months
Isn't this based on an expression from Abigail then at Perlmonks? https://www.masteringperl.org/2013/06/how-abigails-prime-num...
By @gusfoo - 3 months
perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/' <number>

I saw that by Abigail on comp.lang.perl.misc many moons ago. Here is an article about it: http://test.neilk.net/blog/2000/06/01/abigails-regex-to-test...

As far as I know, she was the genesis of this whole thing.