Class: Integer

Inherits:
Object show all
Includes:
Nuggets::Integer::LengthMixin, Nuggets::Integer::MapMixin, Nuggets::Integer::RomanMixin
Defined in:
lib/nuggets/integer/factorial.rb,
lib/nuggets/integer/map.rb,
lib/nuggets/integer/roman.rb,
lib/nuggets/dotted_decimal.rb,
lib/nuggets/integer/length.rb,
lib/nuggets/integer/to_binary_s.rb

Overview

#

nuggets – Extending Ruby #

#

Copyright © 2007-2011 Jens Wille #

#

Authors: #

Jens Wille <[email protected]>                                       #
                                                                        #

nuggets is free software; you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #

#

nuggets is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for # more details. #

#

You should have received a copy of the GNU Affero General Public License # along with nuggets. If not, see <www.gnu.org/licenses/>. #

#

++

Constant Summary collapse

FACTORIAL =

Memoization container: integer => factorial(integer)

{ 0 => 1 }

Constants included from Nuggets::Integer::RomanMixin

Nuggets::Integer::RomanMixin::COMPACT, Nuggets::Integer::RomanMixin::NUMERAL

Instance Method Summary collapse

Methods included from Nuggets::Integer::LengthMixin

#digit_count, #length

Methods included from Nuggets::Integer::RomanMixin

to_roman, #to_roman

Methods included from Nuggets::Integer::MapMixin

#map_positive

Instance Method Details

#factorialObject Also known as: fac, f!

call-seq:

int.factorial => anInteger

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



37
38
39
# File 'lib/nuggets/integer/factorial.rb', line 37

def factorial
  (1..self).inject { |f, i| f * i }
end

#factorial_memoizedObject

call-seq:

int.factorial_memoized => anInteger

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… ;-)



49
50
51
# File 'lib/nuggets/integer/factorial.rb', line 49

def factorial_memoized
  FACTORIAL[self] ||= (1..self).inject { |f, i| FACTORIAL[i] ||= f * i }
end

#to_binary_s(length = nil) ⇒ Object

call-seq:

int.to_binary_s => aString
int.to_binary_s(length) => aString

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



34
35
36
# File 'lib/nuggets/integer/to_binary_s.rb', line 34

def to_binary_s(length = nil)
  "%0#{length}d" % to_s(2)
end

#to_dotted_decimalObject

call-seq:

int.to_dotted_decimal => aString

Converts int to dotted-decimal notation.



35
36
37
# 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('.')
end