Стефан обнови решението на 16.10.2013 11:04 (преди около 11 години)
+class Integer
+ def prime?
+ (2..Math.sqrt(self)).each do |n|
+ return false if self % n == 0
+ end
+ true
+ end
+end
+
+
+class Integer
+ def prime_factors
+ primes = (2..Math.sqrt(self).floor).select { |p| p.prime?}
+ # Не успях да измисля нищо, което
+ # се вписва в ограниченията...
+ end
+end
+
+
+class Array
+ def average
+ total_sum = 0.0
+ self.each {|n| total_sum += n}
+ total_sum / self.size
+ end
+end
+
+
+class Integer
+ def harmonic
+ nth_harmonic = 0
+ (1..self).each do |n|
+ nth_harmonic += Rational(1, n)
+ end
+ nth_harmonic
+ end
+end
+
+
+class Array
+ def drop_every(n)
+ new_array = []
+ (0..self.size-1).each do |i|
+ new_array << self[i] unless (i+1) % n == 0
+ end
+ new_array
+ end
+end
+
+
+class Array
+ def combine_with(other)
+ combined_array = []
+ greater_size = if self.size > other.size then self.size else other.size end
+ (0..greater_size-1).each do |i|
+ combined_array << self[i]
+ combined_array << other[i]
+ end
+ combined_array.compact
+ end
+end
+
+
+class Integer
+ def digits
+ number_of_digits = self.to_s.length
+ integer_to_array = []
+ (0..number_of_digits-1).each do |n|
+ integer_to_array << self / 10**n % 10
+ end
+ integer_to_array.reverse
+ end
+end
+
+
+class Array
+ def frequencies
+ array_to_hash = Hash.new(0)
+ self.each { |n| array_to_hash[n] += 1 }
+ array_to_hash
+ end
+end