Fun hypothetical, or can someone solve? How long, would this regex prime check [1] (via hn 2009) take to run on this on todays average machine?
Hitchhiker's Guide's Deep Thought "42" was 7.5 million years, just for a guide-post.
replies(2):
Hitchhiker's Guide's Deep Thought "42" was 7.5 million years, just for a guide-post.
This algorithm is not practical.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n%i==0:
return False
return True
Haven't tested this, just quickly jotted it down in the HackerNews textarea, but theoretically it should be python code, which checks if n is divisible by any number between 2 and sqrt(n).And wouldn't this algorithm be just O(n), what does the regex engine do differently (except the string overhead)?
(Seriously though, it would make a very good April fools' day PR to recognize that particular regex and trigger a non-trivial prime check routine to match them.)