Comments on snippet: 'Prime number tester'Snipplr comments feed
https://snipplr.com/
Thu, 20 Jan 2022 11:42:31 +0000deepsoul said on 02/Aug/2009
https://snipplr.com/view/6713/prime-number-tester
<p>The test works on unary notation, not decimal. So 2 is written as "11", 3 as "111", 17 as "11111111111111111" and so on. The first branch of the regex matches the empty string (representing 0) and "1" (representing 1), marking them as non-primes. The second branch succeeds if and only if the unary number consists of two or more times two or more 1s, which means it is the product of these two numbers (both at least 2), so not a prime. To print a string of primes and see that this works:
perl -l -e '(1 x $_) !~ /^1?$|^(11+?)\1+$/ & print while ++$_;'
See also: http://www.catb.org/jargon/html/O/one-liner-wars.html</p>Sun, 02 Aug 2009 17:55:24 UTChttps://snipplr.com/view/6713/prime-number-tester