Георги обнови решението на 14.10.2013 15:49 (преди около 11 години)
+class Integer
+ def divisors
+ result = []
+ (1..self.abs).each { |item| result << item if self % item == 0 }
+ result
+ end
+
+ def prime_divisors
+ result = divisors[1..-1]
+ result.delete_if { |item| not item.prime? }
+ end
+
+ def prime?
+ if self == 1
+ false
+ else
+ ([1, self] == divisors)? true : false
+ end
+ end
+
+ def prime_factors
+ return [] if self == 0
+
+ prime_divisors.each do |item|
+ if self % item == 0
+ return [item] + (self / item).prime_factors
+ end
+ end
+ end
+
+ def reciprocal
+ Rational(1, self)
+ end
+
+ def harmonic
+ result = Rational(0)
+ (1..self).each { |i| result += i.reciprocal }
+ result
+ end
+
+ def digits
+ result = []
+ self.abs.to_s.each_char { |char| result << char.ord - '0'.ord }
+ result
+ end
+end
+
+
+class Array
+ def frequencies
+ result = {}
+ self.each { |item| (result.include? item)? result[item] += 1 :
+ result[item] = 1 }
+ result
+ end
+
+ def average
+ self.reduce(:+) / self.length.to_f
+ end
+
+ def drop_every(n)
+ result = []
+ self.each_index do |index|
+ result << self[index] unless (index + 1) % n == 0
+ end
+
+ result
+ end
+
+ def combine_with(other)
+ result = []
+ self.each_index do |index|
+ result << self[index]
+ result << other[index] if other.fetch(index, nil)
+ end
+
+ (other.length > length)? result += other[length..-1] : result
+ end
+end