class Integer



Memoization container: integer => factorial(integer)

Public Instance Methods

Alias for: factorial
Alias for: factorial
factorial → anInteger click to toggle source

Calculate the factorial of int. To use the memoized version: Integer.send(:alias_method, :factorial, :factorial_memoized)

# File lib/nuggets/integer/factorial.rb, line 37
def factorial
  (1..self).inject { |f, i| f * i }
Also aliased as: fac, f!
factorial_memoized → anInteger click to toggle source

Calculate the factorial of int with the help of memoization (Which gives a considerable speedup for repeated calculations – at the cost of memory).

WARNING: Don't try to calculate the factorial this way for “large” integers! This might well bring your system down to its knees… ;-)

# File lib/nuggets/integer/factorial.rb, line 49
def factorial_memoized
  FACTORIAL[self] ||= (1..self).inject { |f, i| FACTORIAL[i] ||= f * i }
to_binary_s → aString click to toggle source
to_binary_s(length) → aString

Returns int as binary number string; optionally zero-padded to length.

# File lib/nuggets/integer/to_binary_s.rb, line 34
def to_binary_s(length = nil)
  "%0#{length}d" % to_s(2)
to_dotted_decimal → aString click to toggle source

Converts int to dotted-decimal notation.

# File lib/nuggets/dotted_decimal.rb, line 35
def to_dotted_decimal
  to_binary_s(32).unpack('a8' * 4).map { |s| s.to_i(2) }.join('.')