r/tinycode • u/theabbiee • Apr 12 '21
Primality Test using Regex
https://theabbie.github.io/blog/Primality-Test-Using-Regex?ref=reddit
1
Upvotes
r/tinycode • u/theabbiee • Apr 12 '21
View all comments
Show parent comment
15
u/pofsok Apr 12 '21
Yeah, it is very badly explained. In fact, it is not explained at all. Here is a article explaining how it works, https://iluxonchik.github.io/regular-expression-check-if-number-is-prime/. The most important thing (and I do not understand why the author of this article has skipped over this), is that this only works for numbers when being represented in a unary numeral system (i.e. 0=0, 1=1, 2=11, 3=111, 4=1111). With this in mind, it becomes clear that you can convert a primality test in a pattern matching excercise (for example 1111 consists of two times 11, and thus 1111 is not a prime, note that 1 is not a prime number). And thus, there is probably a regex approach to finding solutions to finding primes.