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.



301
302
303
304
# File 'lib/epitools/core_ext/numbers.rb', line 301

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

#fact ⇒ Object Also known as: factorial

Factorial



389
390
391
392
393
394
395
396
397
# File 'lib/epitools/core_ext/numbers.rb', line 389

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

#factors ⇒ Object

Returns the all the prime factors of a number.



381
382
383
384
# File 'lib/epitools/core_ext/numbers.rb', line 381

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

#fib ⇒ Object Also known as: fibonacci

Fibonacci (recursive style)



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

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

#invert ⇒ Object

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



415
416
417
# File 'lib/epitools/core_ext/numbers.rb', line 415

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

#prime? ⇒ Boolean

Am I a prime number?

Returns:

  • (Boolean)


358
359
360
# File 'lib/epitools/core_ext/numbers.rb', line 358

def prime?
  Prime.prime? self
end

#primes(starting_at = 2) ⇒ Object

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



365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/epitools/core_ext/numbers.rb', line 365

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: http://www.youtube.com/watch?v=dQw4w9WgXcQ)



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/epitools/core_ext/numbers.rb', line 321

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



351
352
353
# File 'lib/epitools/core_ext/numbers.rb', line 351

def to_base62
  to_base(62)
end

#to_bits ⇒ Object Also known as: bits

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



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

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

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



284
285
286
# File 'lib/epitools/core_ext/numbers.rb', line 284

def to_hex
  "%0.2x" % self
end