2 ms·
The prime number detector is quite clever. It reminds me of the number based logic constructs from Godel's incompleteness theorem. It's essentially maps a mathe
by mustpax 17y ago
The prime number detector is quite clever. It reminds me of the number based logic constructs from Godel's incompleteness theorem. It's essentially maps a mathematical concept to a string and operates on it.
For any unary integer greater than 1, the regular expression /^(11+?)\1+$/ matches it iff the number is not a prime. In other words, if there exists no integer less than the number itself and greater than one (first capturing group) that is a divisor of the number. The non-greedy + is also a nice touch, it will start out from the smallest factors and build from there.
- nazgulnarsil 17y agothat impressed me the most as well. the least lines I could do it in was 4. the others I wasn't necessarily able to think of the one liner before checking the answer but I at least had a general idea of how it might be done. the primes completely stumped me.