Цветан обнови решението на 14.10.2013 23:54 (преди около 11 години)
+class Integer
+ def prime?
+ self > 1 and (2..Math.sqrt(self)).all? { |n| self % n != 0 }
+ end
+
+ def prime_factors
+ self < 0 ? number = -self : number = self
+ factors = []
+ (2..number).each { |divisor| while divisor.prime? and number % divisor == 0
+ factors.push (divisor)
+ number = number / divisor
+ end }
+ factors
+ end
+
+ def harmonic
+ sum = 0
+ (1..self).each { |i| sum = sum + Rational(1,i) }
+ sum
+ end
+
+ def digits
+ digits = []
+ while self / (10**digits.size) > 0
+ digits.push (self % 10**(digits.size+1) / 10**digits.size)
+ end
+ digits.reverse
+ end
+end
+
+class Array
+ def frequencies
+ frequency = {}
+ each { |n| frequency[n] = self.count(n) }
+ frequency
+ end
+
+ def average
+ sum = 0.0
+ each { |n| sum = sum + n }
+ sum/self.size
+ end
+
+ def drop_every (n)
+ dropped_arr = []
+ each_index { |i| dropped_arr.push(self[i]) if (i + 1) % n != 0 }
+ dropped_arr
+ end
+
+ def combine_with(other)
+ combined = []
+ each_index { |i| combined[2 * i] = self[i] }
+ other.each_index { |i| combined[2 * i +1] = other [i] }
+ combined.compact!
+ combined
+ end
+end