by Moyamo on 2/12/15, 4:29 PM with 67 comments
by btilly on 2/12/15, 10:18 PM
However while cute, it is very slow. It tries every possible factorization as a pattern match. When it succeeds, on a string of length n that means that n times it tries to match a string of length n against a specific pattern. This is O(n^2). Try it on primes like 35509, 195341, 526049 and 1030793 and you can observe the slowdown. (Some regular expression engines may employ tricks that speed this up over the naive analysis, but none will be super speedy about it.)
Usual arithmetic is much more efficient. :-)
by anyfoo on 2/12/15, 10:19 PM
If this was a "true" regular expression, in the sense that it describes a regular language (http://en.wikipedia.org/wiki/Regular_language), then checking whether the number is prime or not, by checking if the expression matches, would run in linear time. This would be worth a few nobel prizes at least.
Unfortunately, it is an "extended" regular expression, i.e. not regular at all in the strictest sense, and (as the article says) requires backtracking.
by xyzzyz on 2/13/15, 10:34 AM
It's cool, but it's regexp, not regular expression -- regular expressions cannot test for primeness. The proof is a simple application of pumping lemma[1]: we want to show that language { a^p: p prime } is not regular. Suppose it is. By pumping lemma we have numbers N, n >= 1, such that for every k >= 0, we have a^(N+nk) in our language. But that means that for every k, N+nk is prime, and it fails for k = N, since N+nN = N(1+n) is not a prime, since both N and 1+n are greater than 1.
[1] - http://en.wikipedia.org/wiki/Pumping_lemma_for_regular_langu... [2] - https://news.ycombinator.com/item?id=3391547 [3] - https://news.ycombinator.com/item?id=3391572
by elzr on 2/12/15, 11:08 PM
A high-level description helps makes obvious what's happening:
To check the primality of a number N with string matching,
repeat a string (say, "1") N times
and declare N prime:
UNLESS the repetitions match the string 0 or 1 times
OR the repetitions can be EVENLY matched into groups of MORE than one string.
by andrewflnr on 2/12/15, 10:05 PM
by tom-lord on 2/13/15, 10:30 AM
/this|is|awesome/.examples # => ["this", "is", "awesome"]
So, I tried it out on this "prime number validator" regex: /1?|(11+?)\1+/.examples
#=> ["", "1", "1111", "111111", "11111111"]
/1?|(11+?)\1+/.examples(max_repeater_variance: 50, max_group_results: 100).uniq.sort.map(&:length).first(10)
#=> [0, 1, 4, 6, 8, 9, 10, 12, 14, 15]
require 'prime'
Array(1..100) - /1?|(11+?)\1+/.examples(max_repeater_variance: 50, max_group_results: 3000).uniq.sort.map(&:length) == Prime.first(25)
#=> true
Horribly inefficient, but pretty cool! Here's my gem if case anyone's interested: https://github.com/tom-lord/regexp-examplesby charlesacknin on 2/13/15, 2:01 AM
AFAICT the way the string is going to be parsed is equivalent to this:
def isprime(n):
if n == 0 or n == 1:
return False
i = 2
while i <= n:
if (n - i) % i == 0:
return False
i += 1
return True
A parser implementation may optimize and do "while (n - i) >= 1" instead, which cuts in half the number of iterations.by fxn on 2/13/15, 7:17 AM
https://github.com/fxn/math-with-regexps/blob/master/one-lin...
by nsajko on 2/12/15, 10:50 PM
by logicallee on 2/12/15, 10:36 PM
/^(11+?)\1+$/ is a prime test on whether a unary string > 2 is composite
then people have a real chance of figuring it out. (go ahead and try, if you haven't read the article.)As it stands that 1 x shift is super confusing. who would have thought 1 would become a literal! we wouldn't even write it that way in english.
by bantic on 2/16/15, 11:31 PM
def is_prime(n); str = "5"*n; str !~ /^5?$|^(55+?)\1+$/; end
The regex essentially says, for integer n, "does 2 ones fit evenly into n ones", then "does 3 ones fit into n ones", etc., which is the same as "does 2 fit into n", "does 3 fit into n", etc.
by avinash on 2/13/15, 7:04 AM
When I am explaining the basics of regular expressions to people, I always refer them to this post to make them realize that regular expressions are not only glorified search terms. In general, they are amazed :-)
by ekimekim on 2/13/15, 12:18 PM
by _lce0 on 2/12/15, 10:00 PM
here a JS implementation
function isPrime(n) {
var re = /^1?$|^(11+?)\1+$/;
var s = [];
while (n -- > 0)
s.push(1);
return !re.test(s.join(''));
}
by Amorymeltzer on 2/12/15, 10:23 PM
by TheLoneWolfling on 2/13/15, 5:10 AM
by geertj on 2/12/15, 11:02 PM
by okey on 2/12/15, 11:38 PM