class Integer

Constants

BASE_DIGITS

Cached constants for encoding numbers into bases up to 64

SMALL_POWERS_OF_2

Public Instance Methods

bits()
Alias for: to_bits
blank?() click to toggle source

'true' if the integer is 0

# File lib/epitools/core_ext/truthiness.rb, line 61
def blank?; self == 0; end
fact() click to toggle source

Factorial

# File lib/epitools/core_ext/numbers.rb, line 368
def fact
  if self < 0
    -(1..-self).reduce(:*)
  elsif self == 0
    1
  else
    (1..self).reduce(:*)
  end
end
Also aliased as: factorial
factorial()
Alias for: fact
factors() click to toggle source

Returns the all the prime factors of a number.

# File lib/epitools/core_ext/numbers.rb, line 360
def factors
  Prime # autoload the prime module
  prime_division.map { |n,count| [n]*count }.flatten
end
fib() click to toggle source

Fibonacci (recursive style)

# File lib/epitools/core_ext/numbers.rb, line 382
def fib
  self < 2 ? self : (self-1).fib + (self-2).fib
end
Also aliased as: fibonacci
fibonacci()
Alias for: fib
integer?() click to toggle source
# File lib/epitools/core_ext/truthiness.rb, line 63
def integer?; true; end
invert() click to toggle source

Flip all bits except the sign bit.

NOTE: This method should only be used with unsigned integers; if you use it with a signed integer, it will only flip the non-sign bits (I dunno if that's useful for anything; nothing comes to mind.)

# File lib/epitools/core_ext/numbers.rb, line 394
def invert
  to_s(2).tr("01","10").to_i(2)
end
prime?() click to toggle source

Am I a prime number?

# File lib/epitools/core_ext/numbers.rb, line 337
def prime?
  Prime.prime? self
end
primes(starting_at=2) click to toggle source

Return a specified number of primes (optionally starting at the argument)

# File lib/epitools/core_ext/numbers.rb, line 344
def primes(starting_at=2)
  result  = []
  current = starting_at

  loop do
    if current.prime?
      result << current
      return result if result.size >= self
    end
    current += 1
  end
end
to_base(base=10) click to toggle source

Convert a number into a string representation (encoded in a base <= 64).

The number is represented usiing the characters: 0..9, A..Z, a..z, _, -

(Setting base to 64 results in the encoding scheme that YouTube and url shorteners use for their ID strings, eg: www.youtube.com/watch?v=dQw4w9WgXcQ)

# File lib/epitools/core_ext/numbers.rb, line 300
def to_base(base=10)
  raise "Error: Can't handle bases greater than 64" if base > 64

  n        = self
  digits   = []
  alphabet = BASE_DIGITS[0...base]

  if bits = SMALL_POWERS_OF_2[base]
    # a slightly accelerated version for powers of 2
    mask   = (2**bits)-1

    loop do
      digits << (n & mask)
      n = n >> bits

      break if n == 0
    end
  else
    # generic base conversion
    loop do
      n, digit = n.divmod(base)
      digits << digit

      break if n == 0
    end
  end

  digits.reverse.map { |d| alphabet[d] }.join
end
to_base62() click to toggle source
# File lib/epitools/core_ext/numbers.rb, line 330
def to_base62
  to_base(62)
end
to_bits() click to toggle source

Convert the number to an array of bits (least significant digit first, or little-endian).

# File lib/epitools/core_ext/numbers.rb, line 280
def to_bits
  # TODO: Why does this go into an infinite loop in 1.8.7?
  ("%b" % self).chars.to_a.reverse.map(&:to_i)
end
Also aliased as: bits
to_hex() click to toggle source

Convert the number into a hexadecimal string representation. (Identical to to_s(16), except that numbers < 16 will have a 0 in front of them.)

# File lib/epitools/core_ext/numbers.rb, line 273
def to_hex
  "%0.2x" % self
end