in reply to Simple primality testing

The following Perl 6 implementation probably isn't the fastest you can find, but it certainly one of the shortest:

sub is_prime (Int $n) { $n % all(2 .. sqrt($n)+1); }

Credit: Autrijus in the Apocalypse Now talk.

Replies are listed 'Best First'.
Re^2: Simple primality testing
by ambrus (Abbot) on Nov 22, 2005 at 21:32 UTC

    I don't have perl6 handy, but wouldn't that fail for 2? In that case, sqrt(1) + 1 is greater then 2 so 2 is included in the range.

      You are correct. Nice catch!
Re^2: Simple primality testing
by gu (Beadle) on Nov 27, 2005 at 22:12 UTC