DEV Community

Discussion on: Best Code to check if number is Prime

Collapse
 
jonrandy profile image
Jon Randy 🎖️

In JS, using regex:

const isPrime = x=>!'1'.repeat(x).match(/^1?$|^(11+?)\1+$/)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lionelrowe profile image
lionel-rowe

that's evil, I love it 😍

Collapse
 
hebashakeel profile image
hebaShakeel

Would you like to explain it?

Collapse
 
jonrandy profile image
Jon Randy 🎖️

I didn't write it, but it's fascinating... noulakaz.net/2007/03/18/a-regular-...