Георги обнови решението на 10.10.2013 19:10 (преди около 11 години)
+class Integer
+ def prime?
+ return false if self < 2
+
+ limit = Math.sqrt(self).ceil
+ (2..limit).none? { |x| self != x and self % x == 0 }
+ end
+
+ def prime_factors
+ current = abs
+ (2..current).each_with_object([]) do |x, result|
+ while x.prime? and current % x == 0
+ result << x
+ current /= x
+ end
+ end
+ end
+
+ def harmonic
+ return nil if self <= 0
+ (1..self).map { |x| 1/x.to_r }.reduce(&:+)
+ end
+
+ def digits
+ abs.to_s.chars.each_with_object([]) { |x, result| result << x.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ each_with_object({}) { |x, hash| hash[x] = count(x) }
+ end
+
+ def average
+ return nil if size == 0
+ reduce(&:+) / size.to_f
+ end
+
+ def drop_every(n)
+ result = []
+ each_slice(n) do |x|
+ x.pop
+ result += x
+ end
+
+ result
+ end
+
+ def combine_with(other)
+ return other if empty?
+ return self if other.empty?
+
+ zip(other).flatten.compact
+ end
+end