Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/modint.rb,
lib/core_ext/integer.rb

Overview

Integer

Instance Method Summary collapse

Instance Method Details

#divisorsObject

Returns the positive divisors of self if self is positive.

Example

6.divisors   #=> [1, 2, 3, 6]
7.divisors   #=> [1, 7]
8.divisors   #=> [1, 2, 4, 8]


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/core_ext/integer.rb', line 10

def divisors
  if prime?
    [1, self]
  elsif self == 1
    [1]
  else
    xs = prime_division.map{ |p, n| Array.new(n + 1){ |e| p**e } }
    x = xs.pop
    x.product(*xs).map{ |t| t.inject(:*) }.sort
  end
end

#each_divisor(&block) ⇒ Object

Iterates the given block for each divisor of self.

Example

ds = []
10.divisors{ |d| ds << d }
ds  #=> [1, 2, 5, 10]


28
29
30
# File 'lib/core_ext/integer.rb', line 28

def each_divisor(&block)
  block_given? ? divisors.each(&block) : enum_for(:each_divisor)
end

#to_modintObject Also known as: to_m



7
8
9
# File 'lib/core_ext/modint.rb', line 7

def to_modint
  ModInt.new(self)
end