Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/prime.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.each_prime(ubound, &block) ⇒ Object

Iterates the given block over all prime numbers.

See Prime#each for more details.



122
123
124
# File 'lib/prime.rb', line 122

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. Not recommended for very big integers (> 10**23).

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/prime.rb', line 35

def prime?
  return self >= 2 if self <= 3

  if (bases = miller_rabin_bases)
    return miller_rabin_test(bases)
  end

  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

#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