Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Integer
- def prime?
- num = self.abs
- return false if num == 0 || num == 1
- return true if (1..num-1).inject {|acc, val| (acc * val) % num} == num - 1
- false
- end
- end
- #Uses Wilson's theorem to check for primality.
- #Pros: Is always correct
- #Cons: Even less efficient than trial division - not practical at all
Add Comment
Please, Sign In to add comment