Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysh_mathematica/Overloads/Integer/Prime/is_prime.rb,
lib/rubysh_mathematica/Overloads/Integer/Prime/is_composite.rb,
lib/rubysh_mathematica/Overloads/Integer/Multiples/is_factor_of.rb,
lib/rubysh_mathematica/Overloads/Integer/Multiples/is_multiple_of.rb

Overview

Integer class method overloading.

Instance Method Summary collapse

Instance Method Details

#composite?Boolean

Returns true if self is a composite number.

Returns:

  • (Boolean)


4
5
6
# File 'lib/rubysh_mathematica/Overloads/Integer/Prime/is_composite.rb', line 4

def composite?
  !prime?
end

#factor_of?(number) ⇒ Boolean

Returns true if self is a factor of number.

Returns:

  • (Boolean)


4
5
6
# File 'lib/rubysh_mathematica/Overloads/Integer/Multiples/is_factor_of.rb', line 4

def factor_of?(number)
  zero? ? number.zero? : (number % self).zero?
end

#multiple_of?(number) ⇒ Boolean

Returns true if self is a multiple of number.

Returns:

  • (Boolean)


4
5
6
# File 'lib/rubysh_mathematica/Overloads/Integer/Multiples/is_multiple_of.rb', line 4

def multiple_of?(number)
  number.zero? ? zero? : (self % number).zero?
end

#prime?Boolean

Returns true if self is a prime number.

Returns:

  • (Boolean)


4
5
6
7
# File 'lib/rubysh_mathematica/Overloads/Integer/Prime/is_prime.rb', line 4

def prime?
  return false if [0, 1].include? self
  (2..Math.sqrt(self)).none? { |number| (self % number).zero? }
end