Николай обнови решението на 11.10.2013 02:43 (преди около 11 години)
+class Integer
+ def prime?
+ return false if self <= 1
+
+ (2...self).each { |i| return false if (self % i) == 0 }
+ true
+ end
+
+ def prime_factors
+ factors, a, i = Array.new, abs, 2
+
+ while a > 1 or return factors.sort
+ if i.prime? and a % i == 0
+ factors << i and a /= i and i = 2 # workaround requirements
+ else i += 1
+ end
+ end
+ end
+
+ def harmonic
+ return nil if self < 1
+
+ sum = Rational(0)
+ (1..self).each { |i| sum += Rational(1,i) }
+ sum
+ end
+
+ def digits
+ digit = Array.new
+ n = abs
+
+ while n > 9
+ digit << n % 10
+ n/=10
+ end
+
+ (digit << n).reverse
+ end
+end
+
+class Array
+ def frequencies
+ freq = Hash.new
+ each { |e| freq[e] == nil ? freq[e] = 1 : freq[e] += 1 }
+ freq
+ end
+
+ def average
+ Rational(reduce(:+), length) unless length == 0
+ end
+
+ def drop_every(n)
+ i = 0
+ find_all { |e| (i+=1) % n != 0 } unless length == 0
+ end
+
+ def combine_with(other)
+ combo,i = Array.new,0
+
+ while i < [self.length, other.length].max # self used for clarity
+ combo << self[i] if i < self.length
+ combo << other[i] if i < other.length
+ i += 1
+ end
+ combo
+ end
+end