Димитър обнови решението на 15.10.2013 18:51 (преди около 11 години)
+class Integer
+ def prime?
+ upper_limit = Math.sqrt self
+
+ (2..upper_limit.abs).each { |n| return false if self % n == 0 }
+ true
+ end
+
+ def prime_factors
+ number = abs
+ factors = []
+
+ until number == 1 do
+ factors << (2..number).find { |n| number % n == 0 }
+ number = number / factors.last
+ end
+
+ factors
+ end
+
+ def harmonic
+ reciprocal_numbers = []
+
+ (1..self).each { |n| reciprocal_numbers << Rational(1, n) }
+ reciprocal_numbers.reduce(:+)
+ end
+
+ def digits
+ spliced_digits = []
+ number = abs
+
+ while number % 10 > 0 do
+ spliced_digits << number % 10
+ number = number / 10
+ end
+
+ spliced_digits.reverse
+ end
+end
+
+class Array
+ def frequencies
+ weighted_items = {}
+
+ each do |item|
+ if weighted_items.has_key? item
+ weighted_items[item] = weighted_items[item].next
+ else
+ weighted_items[item] = 1
+ end
+ end
+
+ weighted_items
+ end
+
+ def average
+ reduce(:+).to_f / count
+ end
+
+ def drop_every(n)
+ each_with_index.map { |x, index| x if (index+1) % n != 0 }.compact
+ end
+
+ def combine_with(other)
+ combined_arrays = []
+ zip(other).each { |a, b| combined_arrays << a << b}.flatten.compact
+ end
+end