Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Check for primality with a regexp (See related posts)

Original perl code here:

http://montreal.pm.org/tech/neil_kandalgaonkar.shtml

class Fixnum
  def prime?
    ('1' * self) !~ /^1?$|^(11+?)\1+$/
  end
end

Comments on this post

pediddle posts on Mar 18, 2007 at 14:01
That is really nifty. Slow as all hell I'd wager, though. Exponential time?

You need to create an account or log in to post comments to this site.


Click here to browse all 5141 code snippets

Related Posts