Кристиян обнови решението на 14.10.2013 16:08 (преди около 11 години)
+class Integer
+ def prime?
+ if self <= 1
+ return false
+ end
+ (2..Math.sqrt(self)).each do |i|
+ if self % i == 0
+ return false
+ end
+ end
+ true
+ end
+ def prime_factors
+ array, number = [], (self > 0 ? self : -self)
+ (2..number).each do |i|
+ while number % i == 0
+ array << i
+ number /= i
+ end
+ end
+ array
+ end
+ def harmonic
+ rational = Rational(0, 1)
+ (1..self).each do |i|
+ rational += Rational(1, i)
+ end
+ rational
+ end
+ def digits
+ array, number = [], (self > 0 ? self : -self)
+ while number != 0
+ array << number % 10
+ number /=10
+ end
+ self == 0 ? nil : array.reverse
+ end
+end
+
+class Array
+ def frequencies
+ hash = {}
+ each do |i|
+ if hash.has_key?(i)
+ hash[i] += 1
+ else
+ hash[i] = 1
+ end
+ end
+ hash
+ end
+ def average
+ sum, count = 0.0, 0
+ each do |i|
+ sum += i
+ count += 1
+ end
+ sum / count
+ end
+ def drop_every(n)
+ array = n != 0 ? [] : self
+ each do |i|
+ if n != 0 and (index(i) + 1) % n != 0
+ array << i
+ end
+ end
+ self.empty? ? nil : array
+ end
+ def combine_with(other)
+ array = []
+ len = self.length < other.length ? other.length : self.length
+ (0..len-1).each do |i|
+ array << self[i]
+ array << other[i]
+ end
+ array.compact
+ end
+end