Иван обнови решението на 15.10.2013 21:41 (преди около 11 години)
+class Integer
+ def prime?
+ return true if self == 2
+ return false if self.even? or self <= 1
+ sqrt = Math.sqrt(self).to_i
+ (2..sqrt).step(2).none?{|divisor| self.modulo(divisor).zero?}
+ end
+
+ def prime_factors (previous_divisor=2)
+ return nil if self.abs <= 1
+ sqrt = Math.sqrt(self.abs)
+ divisor = (previous_divisor..sqrt).find{|x| self.abs.modulo(x).zero?}
+ divisor ? [divisor] + (self.abs/divisor).prime_factors(divisor) : [self.abs]
+ end
+
+ def digits
+ self.to_s.split('').map{|digit| digit.to_i }
+ end
+
+ def harmonic
+ (2..self).inject(1){|sum, x| sum + 1/x.to_r}
+ end
+end
+
+class Array
+ def frequencies
+ freq = Hash.new(0)
+ self.each{|item| freq[item] += 1}
+ freq
+ end
+
+ def average
+ self.inject(:+)/self.length.to_f
+ end
+
+ def drop_every (n)
+ (0...self.length).step(n).inject([]) {|result, i| result + self[i...i+n-1]}
+ end
+
+ def combine_with (other)
+ len = [self.length, other.length]
+ (0...len.min).inject([]){|sh, index| sh << self[index] << other[index]} +
+ (len.min...len.max).map{|index| self[index] or other[index]}
+ end
+end