rosettacode: primality by trial division
authorBlackMinou <romain.chanoir@viacesi.fr>
Wed, 10 Jun 2015 07:38:12 +0000 (09:38 +0200)
committerBlackMinou <romain.chanoir@viacesi.fr>
Wed, 10 Jun 2015 11:40:36 +0000 (13:40 +0200)
Signed-off-by: BlackMinou <romain.chanoir@viacesi.fr>

lib/standard/math.nit

index 8b5fecb..499455c 100644 (file)
@@ -84,6 +84,24 @@ redef class Int
        #     assert not 13.is_even
        fun is_odd: Bool do return not is_even
 
+       # Is self a prime number ?
+       #
+       # assert 3.is_prime
+       # assert not 1.is_prime
+       # assert not 12.is_prime
+       fun is_prime: Bool
+       do
+               if self == 2 then
+                       return true
+               else if self <= 1 or self.is_even then
+                       return false
+               end
+               for i in [3..self.sqrt[ do
+                       if self % i == 0 then return false
+               end
+               return true
+       end
+
        # Returns the `self` raised to the power of `e`.
        #
        #     assert 2 ** 3 == 8