Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/core_ext/modint.rb,
lib/core_ext/integer.rb
Overview
Integer
Instance Method Summary collapse
-
#divisors ⇒ Object
Returns the positive divisors of
self
ifself
is positive. -
#each_divisor(&block) ⇒ Object
Iterates the given block for each divisor of
self
. - #to_modint ⇒ Object (also: #to_m)
Instance Method Details
#divisors ⇒ Object
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 |