Росен обнови решението на 14.10.2013 08:57 (преди около 11 години)
+class Integer < Numeric
+ def prime?
+
+ (2...self).map { |a| self % a != 0 }.all? if self > 0
+
+ end
+
+ def prime_factors
+
+ return [] if self.abs == 1
+
+ factor = (2..self.abs).find { |a| self % a == 0 }
+ [factor].concat((self.abs / factor).prime_factors)
+
+ end
+
+ def harmonic
+
+ return 1.to_r if self == 1
+ #sum = 1 / self.to_r + 1 / (self - 1).harmonic
+ 1.upto(self).inject { |a, b| a.to_r + (1/b.to_r) }
+
+ end
+
+ def digits
+
+ return [self.abs] if self.abs < 10
+
+ (self.abs / 10).digits.concat [self.abs % 10]
+
+ end
+
+end
+
+class Array
+
+ def frequencies
+ frequencies = Hash.new(0)
+
+ each { |a| frequencies[a] += 1 }
+
+ return frequencies
+ end
+
+ def average
+ inject { |a, b| a + b}.to_f / length
+ end
+
+ def drop_every(n)
+ result = []
+ result.replace self
+
+ (n - 1).step(result.size - 1, n - 1) { |i| result.delete_at i }
+
+ return result
+ end
+
+ def combine_with(other)
+ result = []
+
+ if size > other.size
+ 0.upto(other.size - 1).each { |i| result += [self[i]] + [other[i]] }
+ result += self.drop(result.size - 1)
+ else
+ 0.upto(size - 1).each { |i| result += [self[i]] + [other[i]] }
+ result += other.drop(result.size - 1)
+ end
+ end
+
+end