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


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

def blank?; self == 0; end

#factObject Also known as: factorial

Factorial


350
351
352
353
354
355
356
357
358
# File 'lib/epitools/core_ext/numbers.rb', line 350

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.


342
343
344
345
# File 'lib/epitools/core_ext/numbers.rb', line 342

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

#fibObject Also known as: fibonacci

Fibonacci (recursive style)


364
365
366
# File 'lib/epitools/core_ext/numbers.rb', line 364

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

#integer?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.)


376
377
378
# File 'lib/epitools/core_ext/numbers.rb', line 376

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

#prime?Boolean

Am I a prime number?


319
320
321
# File 'lib/epitools/core_ext/numbers.rb', line 319

def prime?
  Prime.prime? self
end

#primes(starting_at = 2) ⇒ Object

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


326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/epitools/core_ext/numbers.rb', line 326

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)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/epitools/core_ext/numbers.rb', line 282

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


312
313
314
# File 'lib/epitools/core_ext/numbers.rb', line 312

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


262
263
264
265
# File 'lib/epitools/core_ext/numbers.rb', line 262

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


255
256
257
# File 'lib/epitools/core_ext/numbers.rb', line 255

def to_hex
  "%0.2x" % self
end