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

#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


368
369
370
371
372
373
374
375
376
# 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

#factorsObject

Returns the all the prime factors of a number.


360
361
362
363
# 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

#fibObject Also known as: fibonacci

Fibonacci (recursive style)


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

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


394
395
396
# File 'lib/epitools/core_ext/numbers.rb', line 394

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

#prime?Boolean

Am I a prime number?

Returns:

  • (Boolean)

337
338
339
# File 'lib/epitools/core_ext/numbers.rb', line 337

def prime?
  Prime.prime? self
end

#primes(starting_at = 2) ⇒ Object

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


344
345
346
347
348
349
350
351
352
353
354
355
# 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) ⇒ 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)


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# 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_base62Object


330
331
332
# File 'lib/epitools/core_ext/numbers.rb', line 330

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