Николай обнови решението на 13.10.2013 23:37 (преди около 11 години)
+class Integer
+ def prime?
+ self > 1 and (2..Math.sqrt(self)).none? { |div| remainder(div) == 0 }
+ end
+
+ def prime_factors
+ value = (2...abs).find { |div| abs.remainder(div) == 0 }
+ value ? [value] + (abs / value).prime_factors : [abs]
+ end
+
+ def harmonic
+ (1..self).reduce { |sum,number| sum + Rational(1,number) }.to_r
+ end
+
+ def digits
+ abs.to_s.chars.map { |number| number.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ reduce(Hash.new(0)) { |hash,key| hash if hash[key] += 1 }
+ end
+
+ def average
+ reduce { |sum,number| sum + number } / count.to_f
+ end
+
+ def drop_every(n)
+ select.each_with_index { |val,index| (index + 1).remainder(n) != 0 }
+ end
+
+ #OPTIMIZE
+ def combine_with(other)
+ list = []
+
+ until count == 0 and other.count == 0 do
+ list << shift unless empty?
+ list << other.shift unless other.empty?
+ end
+
+ list
+ end
+end