Александър обнови решението на 16.10.2013 03:09 (преди около 11 години)
+class Integer
+ def prime?
+ self < 2 and return false
+ iter, temp = 2, self
+ while iter < temp do
+ self % iter == 0 and return false
+ iter, temp = [iter+1, self/iter]
+ end
+ return true
+ end
+end
+
+class Integer
+ def prime_factors
+ temp, array = self.abs, Array.new
+ (1..temp).each do |iter|
+ while temp % iter == 0 and iter.prime?
+ temp = temp / iter
+ array.push iter
+ end
+ end
+ return array
+ end
+end
+
+class Integer
+ def harmonic
+ temp, sum = self, 0
+ while temp != 0
+ sum, temp = [sum + Rational(1, temp), temp - 1]
+ end
+ return sum
+ end
+end
+
+class Integer
+ def digits
+ temp, array = self.abs, Array.new
+ while temp / 10 >= 1
+ array.push temp%10
+ temp = temp/10
+ end
+ array.push temp%10
+ return array.reverse
+ end
+end
+
+class Array
+ def frequencies
+ temp, hash = self.clone, Hash.new
+ temp.each do |iter|
+ if !hash.has_key?(iter)
+ hash[iter] = temp.count(iter)
+ end
+ end
+ return hash
+ end
+end
+
+class Array
+ def average
+ temp, sum = self.clone, 0
+ temp.each do |iter|
+ sum = sum + iter
+ end
+ return sum.to_f/temp.count.to_f
+ end
+end
+
+class Array
+ def drop_every(index_to_skip)
+ counter, array = 1, Array.new
+ self.each do |iter|
+ if counter%index_to_skip != 0
+ array.push iter
+ end
+ counter = counter + 1
+ end
+ return array
+ end
+end
+
+class Array
+ def combine_with(second_array)
+ array, longest_length = Array.new, [self.length, second_array.length].max
+ (0..longest_length).each do |index|
+ if index < self.length then array.push self[index] end
+ if index < second_array.length then array.push second_array[index] end
+ end
+ return array
+ end
+end