Class: Integer
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/prime.rb |
Class Method Summary
-
.each_prime(ubound, &block)
Iterates the given block over all prime numbers.
-
.from_prime_division(pd)
Re-composes a prime factorization and returns the product.
Instance Attribute Summary
-
#prime? ⇒ Boolean
readonly
Returns true if
self
is a prime number, else returns false.
Instance Method Summary
-
#prime_division(generator = Prime::Generator23.new)
Returns the factorization of
self
.
Class Method Details
.each_prime(ubound, &block)
Iterates the given block over all prime numbers.
See ::Prime#each for more details.
.from_prime_division(pd)
Re-composes a prime factorization and returns the product.
See Prime#int_from_prime_division for more details.
# File 'lib/prime.rb', line 22
def Integer.from_prime_division(pd) Prime.int_from_prime_division(pd) end
Instance Attribute Details
#prime? ⇒ Boolean
(readonly)
Returns true if self
is a prime number, else returns false.
# File 'lib/prime.rb', line 34
def prime? return self >= 2 if self <= 3 return true if self == 5 return false unless 30.gcd(self) == 1 (7..Integer.sqrt(self)).step(30) do |p| return false if self%(p) == 0 || self%(p+4) == 0 || self%(p+6) == 0 || self%(p+10) == 0 || self%(p+12) == 0 || self%(p+16) == 0 || self%(p+22) == 0 || self%(p+24) == 0 end true end
Instance Method Details
#prime_division(generator = Prime::Generator23.new)
Returns the factorization of self
.
See Prime#prime_division for more details.
# File 'lib/prime.rb', line 29
def prime_division(generator = Prime::Generator23.new) Prime.prime_division(self, generator) end