Илия обнови решението на 16.10.2013 15:56 (преди около 11 години)
+class Integer
+ def prime?
+ self > 1 and ('1' * self) !~ /^(11+?)\1+$/
+ end
+
+ def prime_factorization(natural_number)
+ return [] if natural_number == 1
+ prime_factor = (2..natural_number).find { |x| natural_number % x == 0 }
+ [prime_factor] + prime_factorization(natural_number / prime_factor)
+ end
+
+ def prime_factors
+ prime_factorization(self.abs)
+ end
+
+ def harmonic
+ (1..self).inject(0){ |sum,n| sum + Rational(1) / n }
+ end
+
+ def digits
+ self.abs.to_s.chars.map(&:to_i)
+ end
+end
+
+class Array
+ def frequencies
+ elements_frequency = Hash.new(0)
+ self.each { |element| elements_frequency[element] += 1 }
+ elements_frequency
+ end
+
+ def average
+ self.inject(:+).to_f / self.count
+ end
+
+ def drop_every(n)
+ remained_indexes = (1..self.count).select { |a| a % n != 0 }
+ remained_indexes.map { |a| self[a - 1] }
+ end
+
+ def combine_with(other)
+ self.zip(other).flatten.compact + Array(other[self.count..-1])
+ end
+end