Class: Integer

Inherits:
Object show all
Defined in:
lib/backports/1.8.7/integer/odd.rb,
lib/backports/1.8.7/integer/ord.rb,
lib/backports/1.8.7/integer/even.rb,
lib/backports/1.8.7/integer/pred.rb,
lib/backports/1.9.1/stdlib/prime.rb,
lib/backports/2.5.0/integer/sqrt.rb,
lib/backports/1.9.1/integer/round.rb,
lib/backports/2.5.0/integer/nobits.rb,
lib/backports/2.5.0/integer/allbits.rb,
lib/backports/2.5.0/integer/anybits.rb,
lib/backports/3.2.0/integer/ceildiv.rb,
lib/backports/3.1.0/integer/try_convert.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.



48
49
50
# File 'lib/backports/1.9.1/stdlib/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/backports/1.9.1/stdlib/prime.rb', line 22

def Integer.from_prime_division(pd)
  Prime.int_from_prime_division(pd)
end

.sqrt(n) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/backports/2.5.0/integer/sqrt.rb', line 6

def self.sqrt(n)
  n = Backports.coerce_to_int(n)
  return Math.sqrt(n).to_i if n <= 9_999_899_999_899_999_322_536_673_279
  bits_shift = n.bit_length / 2 + 1
  bitn_mask = root = 1 << bits_shift
  loop do
    root ^= bitn_mask if (root * root) > n
    bitn_mask >>= 1
    return root if bitn_mask == 0
    root |= bitn_mask
  end
end

.try_convert(obj) ⇒ Object



4
5
6
# File 'lib/backports/3.1.0/integer/try_convert.rb', line 4

def self.try_convert(obj)
  ::Backports.try_convert(obj, ::Integer, :to_int)
end

Instance Method Details

#allbits?(n) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
# File 'lib/backports/2.5.0/integer/allbits.rb', line 4

def allbits?(n)
  n = Backports.coerce_to_int(n)
  n & self == n
end

#anybits?(n) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
# File 'lib/backports/2.5.0/integer/anybits.rb', line 4

def anybits?(n)
  n = Backports.coerce_to_int(n)
  n & self != 0
end

#ceildiv(arg) ⇒ Object



3
4
5
# File 'lib/backports/3.2.0/integer/ceildiv.rb', line 3

def ceildiv(arg)
  fdiv(arg).ceil
end

#even?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/backports/1.8.7/integer/even.rb', line 3

def even?
  self[0].zero?
end

#nobits?(n) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
# File 'lib/backports/2.5.0/integer/nobits.rb', line 4

def nobits?(n)
  n = Backports.coerce_to_int(n)
  n & self == 0
end

#odd?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/backports/1.8.7/integer/odd.rb', line 3

def odd?
  !self[0].zero?
end

#ordObject



3
4
5
# File 'lib/backports/1.8.7/integer/ord.rb', line 3

def ord
  self
end

#predObject



3
4
5
# File 'lib/backports/1.8.7/integer/pred.rb', line 3

def pred
  self - 1
end

#prime?Boolean

Returns true if self is a prime number, else returns false.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/backports/1.9.1/stdlib/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/backports/1.9.1/stdlib/prime.rb', line 29

def prime_division(generator = Prime::Generator23.new)
  Prime.prime_division(self, generator)
end

#round_with_digits(ndigits = 0) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/backports/1.9.1/integer/round.rb', line 6

def round_with_digits(ndigits=0)
  ndigits = Backports::coerce_to_int(ndigits)
  case
  when ndigits.zero?
    self
  when ndigits > 0
    raise RangeError if ndigits >= 1<<31
    Float(self)
  else
    pow = 10 ** (-ndigits)
    return 0 if pow.is_a?(Float) # when ndigits hugely negative
    remain = self % pow
    comp = self < 0 ? :<= : :<
    remain -= pow unless remain.send(comp, pow / 2)
    self - remain
  end
end