^1?$|^(11+?)\1+$I don't know who to credit (?) for this monstrosity, but it's been floating around for quite a while. I first ran into it about 15 years ago. To test an integer for primality, you first convert the integer to a string of 1s (e.g., 5 becomes 11111, 7 becomes 1111111, etc.). Then you see if the regular expression matches. If it does, the number is not prime. If the match fails, the number is prime. I read a decent explanation of how it works this morning.
So why do I call this a monstrosity? Well, it's a bit like a carpenter using repeated hammer blows to cut a piece off a 2x4, instead of using a saw. It's the wrong tool. The hammer is very useful – for driving nails or punishing regular expression abusers – but it's not good for cutting a piece of wood...
No comments:
Post a Comment