Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Does anybody know how to do math with regex? (triples)

And conditionals don't seem to work?



From: http://quaxio.com/triple/

    ^([0369]|[258][0369]*[147]|[147]([0369]|[147][0369]*[258])*[258]|[258][0369]*[258]([0369]|[147][0369]*[258])*[258]|[147]([0369]|[147][0369]*[258])*[147][0369]*[147]|[258][0369]*[258]([0369]|[147][0369]*[258])*[147][0369]*[147])*$


The idea is as thus:

Remember division rules? If the sum of digits is divisible by 3, then the number is divisible by 3

So, if you have 0, 3, 6, 9, it's as you can remove them, the value won't change

Remaining are the 2, 3, etc digit numbers divisible by 3


569pts: ([^31]0|31|[017]2|[03]03|[^1]4|(900|01|7)5|6|[48]7|[57]8|09)$


Originally had 568, then saw this and improved. :)

580pts: 00(0$|3|6|9|12|15)|[^0]14|.53|^3[^38]|55|43|23|9.7


Nice. A smidge better at 582:

    5[54]|2[437]|00($|[369]|1[25])|^8[17]|^3[29]|9.7


One more smidgen, behold 584:

    5[54]|2[437]|00($|[369]|1[25])|^[83][1729]|9.7


586:

    ^[378][12479]|00($|[369]|1[25])|5[45]|2[347]


589: ^[378][12479]|00($|[369]|1[25])|55|2[347]


My guess is that 147 must appear the same number of times as 258, but I'm not sure if that's even expressible in regexp.


Refined: [0369] can appear any number of times [147] and [258] must appear an equal number of times, or for any remaining: [147] must appear a multiple of 3 times [258] must appear a multiple of 3 times


No. 111 is a multiple of 3.


Sadly that wouldn't work for: 140091876 147 = 4 times 258 = 1 time


I got 187 with this

  [369](?![7238])|(?:0)12
It's sloppy and certainly not correct, but a decent score.


^[01349][064] works for 197




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: