Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby isPrime Method

Tags:

regex

ruby

primes

('1' * N) !~ /^1?$|^(11+?)\1+$/

On the net, I found this piece of Ruby code that works for N >= 0 that determines whether or not N is a prime. From what I can tell, it looks like play with regex but I have no idea how it works. Could someone tell me how it works?


1 Answers

You can find a lengthy explanation of this code here: http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/

like image 55
Jay Avatar answered Sep 15 '25 05:09

Jay