Сияна обнови решението на 15.10.2013 21:57 (преди около 11 години)
+class Integer
+ def prime?
+ (2..self.abs - 1).each do |i|
+ return false if (self.abs % i == 0)
+ end
+ true
+ end
+
+ def prime_factors
+ number = self.abs and prime_factors_array = []
+ (2..number).each do |i|
+ while number % i == 0
+ prime_factors_array << i
+ number = number/i
+ end
+ end
+ prime_factors_array
+ end
+
+ def harmonic
+ harmonic_sum = 0
+ (1..self).each do |i|
+ harmonic_sum = harmonic_sum.to_r + 1.to_r / i.to_r
+ end
+ harmonic_sum.to_r
+ end
+
+ def digits
+ self < 0 ? length = self.to_s.length - 2 : length = self.to_s.length - 1
+ array_of_digits = [] and number = self.abs
+ while length >= 0
+ array_of_digits << number / (10 ** length)
+ number = number % (10 ** length) and length = length - 1
+ end
+ array_of_digits
+ end
+end
+class Array
+ def frequencies
+ frequencies_hash = {}
+ self.each do |element|
+ frequence_number = self.find_all { |current_item| current_item == element }
+ frequencies_hash[element] = frequence_number.length
+ end
+ frequencies_hash
+ end
+
+ def average
+ average_sum = 0.0
+ self.each { |i| average_sum = average_sum + i }
+ average_sum/self.length
+ end
+
+ def drop_every(n)
+ array = []
+ (0..self.length - 1).each do |i|
+ array << self[i] if (i % n == 0)
+ end
+ array
+ end
+
+ def combine_with(other)
+ combined_array = [] and i = 0
+ other.each do |item|
+ combined_array << self[i] << item if i < self.length
+ i+=1
+ end
+ (i..self.length - 1).each { |item| combined_array << self[item] }
+ combined_array
+ end
+end