3 ms·
Earlier today I read a blog post (via http://news.ycombinator.com/item?id=1937062 http://news.ycombinator.com/item?id=1937062), discussing the problem of findin
by robinhouston 16y ago
Earlier today I read a blog post
(via http://news.ycombinator.com/item?id=1937062 http://news.ycombinator.com/item?id=1937062), discussing
the problem of finding a regular expression that matches
the binary numbers that are divisible by 3.
The author of that post solved it more or less by trial-and-error,
but – as some commenters on that thread already pointed out – there’s
actually a fairly straightforward algorithm that works for any divisor
in any base.
I thought it would be a fun exercise to implement the algorithm
in Javascript, and here is the result. It's quite fun to play with.
Even though (obviously) I understand how it works, it's still
somehow surprising to see a regular expression that matches all
the multiples of 7 in base 10, for example. (That example is quite
a hairy one!)