Мария обнови решението на 14.10.2013 15:51 (преди около 11 години)
+class Integer
+ def prime?
+ 2.upto(self - 1).all? { |n| self.remainder(n).nonzero? }
+ end
+
+ def prime_factors
+ array, number = [], self.abs
+ 2.upto(number).each do |n|
+ number /= n and array << n while number.remainder(n).zero?
+ end
+ return array
+ end
+
+ def harmonic
+ 0.upto(self).inject { |sum, value| sum + 1 / Rational(value) }
+ end
+
+ def digits
+ self.abs.to_s.split('').map { |n| n.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ hash = Hash.new(0)
+ self.each { |value| hash[value] += 1 }
+ return hash
+ end
+
+ def average
+ self.reduce(:+).to_f / self.size
+ end
+
+ def drop_every(n)
+ self.select.with_index { |_, index| (index + 1).remainder(n).nonzero? }
+ end
+
+ def combine_with(other)
+ self.zip(other).flatten.compact
+ end
+end