Heykuki News

TopNewBestAskShowJobs
TopNewBestAskShowJobs
Show HN: Prime Number Regex
5 points
Cieplak
8 years ago
I was messing around representing integers using binary trees, and noticed that a certain tree format makes it trivial to grep for prime numbers. Probably not very novel, but thought y'all might enjoy it. Here's one through sixteen:

    * (()())
    * ((()())())
    * (((()())())())
      (((()())())(()()))
    * ((((()())())())())
      ((((()())())())(()()))
    * ((((()())())(()()))())
      ((((()())())(()()))(()()))
      ((((()())())(()()))((()())()))
      ((((()())())(()()))(((()())())()))
    * (((((()())())())())())
      (((((()())())())())(()()))
    * (((((()())())())(()()))())
      (((((()())())())(()()))(()()))
      (((((()())())())(()()))((()())()))
      (((((()())())())(()()))(((()())())()))
I added asterisks in front of the primes. Here's the set of 16-bit ints:

https://gist.github.com/cieplak/213fb4c88581d8971b274b7ec647...

The regex just checks if the string ends with '())':

    cat integers.txt | grep '())$' | nl | tail -1