class Integer

Public Instance Methods

fermat(n) click to toggle source
# File lib/primality.rb, line 26
def fermat(n)
  if n == 2 ; return true end
  if n == 3 ; return true end
  (2...n).to_a.sample(n).each do |a|
    if n.gcd(a) != 1 or a.pow(n - 1, n).to_i % n != 1 ; return false end
  end
  return true
end
ortho(n) click to toggle source
# File lib/primality.rb, line 19
def ortho(n)
  (2...Math::sqrt(n).to_i+1).each do |value|
    if n % value == 0 ; return false end
  end
  return true
end
prime?(method='ortho') click to toggle source
# File lib/primality.rb, line 9
def prime?(method='ortho')
  if self <= 1
    raise InvalidNumberError.new("#{self} must be 2 or more")
  elsif ['ortho', 'fermat'].include?(method)
    return self.public_send(method, self)
  else
    raise UndefinedPrimalityTestMethod.new("#{method} is not defined method")
  end
end