Class: Integer

Inherits:
Object show all
Defined in:
lib/epitools/core_ext/numbers.rb,
lib/epitools/core_ext/truthiness.rb

Constant Summary collapse

BASE_DIGITS =

Cached constants for encoding numbers into bases up to 64

[*'0'..'9', *'A'..'Z', *'a'..'z', '_', '-']
SMALL_POWERS_OF_2 =
{2=>1, 4=>2, 8=>3, 16=>4, 32=>5, 64=>6}

Instance Method Summary collapse

Instance Method Details

#binary(min_length = 8) ⇒ Object Also known as: bin

Convert this number to a Ruby-style string of platform-endian binary digits (eg: “0b011010101”). Note: The ‘min_length` argument tells how long the string should be (padding the missing digits with 0’s); defaults to 8.



290
291
292
293
# File 'lib/epitools/core_ext/numbers.rb', line 290

def binary(min_length=8)
  width = (log(2).ceil / min_length.to_f).ceil * min_length
  "0b%0.#{width}b" % self
end

#blank?Boolean

‘true’ if the integer is 0

Returns:

  • (Boolean)


61
# File 'lib/epitools/core_ext/truthiness.rb', line 61

def blank?; self == 0; end

#factObject Also known as: factorial

Factorial



378
379
380
381
382
383
384
385
386
# File 'lib/epitools/core_ext/numbers.rb', line 378

def fact
  if self < 0
    -(1..-self).reduce(:*)
  elsif self == 0
    1
  else
    (1..self).reduce(:*)
  end
end

#factorsObject

Returns the all the prime factors of a number.



370
371
372
373
# File 'lib/epitools/core_ext/numbers.rb', line 370

def factors
  Prime # autoload the prime module
  prime_division.map { |n,count| [n]*count }.flatten
end

#fibObject Also known as: fibonacci

Fibonacci (recursive style)



392
393
394
# File 'lib/epitools/core_ext/numbers.rb', line 392

def fib
  self < 2 ? self : (self-1).fib + (self-2).fib
end

#integer?Boolean

Returns:

  • (Boolean)


63
# File 'lib/epitools/core_ext/truthiness.rb', line 63

def integer?; true; end

#invertObject

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.)



404
405
406
# File 'lib/epitools/core_ext/numbers.rb', line 404

def invert
  to_s(2).tr("01","10").to_i(2)
end

#prime?Boolean

Am I a prime number?

Returns:

  • (Boolean)


347
348
349
# File 'lib/epitools/core_ext/numbers.rb', line 347

def prime?
  Prime.prime? self
end

#primes(starting_at = 2) ⇒ Object

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



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/epitools/core_ext/numbers.rb', line 354

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) ⇒ Object

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)



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/epitools/core_ext/numbers.rb', line 310

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_base62Object



340
341
342
# File 'lib/epitools/core_ext/numbers.rb', line 340

def to_base62
  to_base(62)
end

#to_bitsObject Also known as: bits

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



280
281
282
283
# 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

#to_hexObject

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.)



273
274
275
# File 'lib/epitools/core_ext/numbers.rb', line 273

def to_hex
  "%0.2x" % self
end