Томислав обнови решението на 15.10.2013 13:48 (преди около 11 години)
+class Integer
+ def prime
+ (2..self.abs - 1).each do |smaller_num|
+ if self % smaller_num == 0 then return false end
+ end
+ true
+ end
+
+ def prime_factors
+ simple_multipliers = if self.prime then [1] else [] end
+ (2..num_copy = self.abs).each do |multiplier|
+ until num_copy % multiplier != 0 or num_copy == 0 or !multiplier.prime do
+ num_copy = num_copy / multiplier
+ simple_multipliers.push(multiplier)
+ end
+ end
+ simple_multipliers
+ end
+
+ def harmonic
+ sum = Rational(1,1)
+ (2..self).each {|elem| sum += Rational(1,elem)}
+ sum
+ end
+
+ def digits
+ array_digits = []
+ num_copy = self.abs
+ begin
+ array_digits.push(num_copy % 10)
+ num_copy = num_copy / 10
+ end while num_copy != 0
+ array_digits.reverse
+ end
+end
+
+class Array
+ def frequencies
+ array_hash = {}
+ self.each do |array_elem|
+ array_hash[array_elem] = if array_hash.has_key?(array_elem)
+ array_hash[array_elem] + 1
+ else
+ 1
+ end
+ end
+ array_hash
+ end
+
+ def average
+ sum = 0.0
+ self.each {|elem| sum += elem}
+ return sum / self.length
+ end
+
+ def drop_every(n)
+ new_array = []
+ (1..self.length).each do |i|
+ if i % n != 0 then new_array.push(self[i - 1]) end
+ end
+ new_array
+ end
+
+ def combine_with(other)
+ combined_array = []
+ (0..self.length + other.length - 1).each do |i|
+ combined_array.push(self[i]) if self.fetch(i, -1) != -1
+ combined_array.push(other[i]) if other.fetch(i, -1) != -1
+ end
+ combined_array
+ end
+end