Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/prime.rb
Class Method Summary collapse
-
.each_prime(ubound, &block) ⇒ Object
Iterates the given block over all prime numbers.
-
.from_prime_division(pd) ⇒ Object
Re-composes a prime factorization and returns the product.
Instance Method Summary collapse
-
#prime? ⇒ Boolean
Returns true if
self
is a prime number, else returns false. -
#prime_division(generator = Prime::Generator23.new) ⇒ Object
Returns the factorization of
self
.
Class Method Details
.each_prime(ubound, &block) ⇒ Object
Iterates the given block over all prime numbers.
See Prime
#each for more details.
48 49 50 |
# File 'lib/prime.rb', line 48 def Integer.each_prime(ubound, &block) # :yields: prime Prime.each(ubound, &block) end |
.from_prime_division(pd) ⇒ Object
Re-composes a prime factorization and returns the product.
See Prime#int_from_prime_division for more details.
22 23 24 |
# File 'lib/prime.rb', line 22 def Integer.from_prime_division(pd) Prime.int_from_prime_division(pd) end |
Instance Method Details
#prime? ⇒ Boolean
Returns true if self
is a prime number, else returns false.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/prime.rb', line 34 def prime? return self >= 2 if self <= 3 return false if self % 2 == 0 or self % 3 == 0 (5..(self**0.5).floor).step(6).each do |i| if self % i == 0 || self % (i + 2) == 0 return false end end true end |
#prime_division(generator = Prime::Generator23.new) ⇒ Object
Returns the factorization of self
.
See Prime#prime_division for more details.
29 30 31 |
# File 'lib/prime.rb', line 29 def prime_division(generator = Prime::Generator23.new) Prime.prime_division(self, generator) end |