Константин обнови решението на 16.10.2013 02:28 (преди около 11 години)
+class Integer
+ def prime?
+ if self<0 then return false end
+ is_divisible = false
+ (2...self).each { |i| is_divisible = (((self % i) == 0) or is_divisible) }
+ not is_divisible
+ end
+
+ def list_of_primes_up_to_self
+ list_of_primes = []
+ (2...self).each { |i| (list_of_primes << i) if i.prime? }
+ list_of_primes
+ end
+
+ def prime_factors
+ new_self = self.abs
+ primes = new_self.list_of_primes_up_to_self
+ factors = []
+ primes.each {|i| while new_self%i == 0 do new_self/=i and factors << i end}
+ factors.to_s
+ end
+
+ def harmonic
+ list_of_rationals = (1..self).map {|i| Rational(1,i)}
+ accumulator = 0
+ list_of_rationals.each {|i| accumulator += i}
+ accumulator
+ end
+
+ def abs
+ self<0 ? -self : self
+ end
+
+ def digits
+ list = []
+ new_self = self.abs
+ new_self.to_s.split('').map {|i| list << i.to_s }
+ list.map {|i| i.to_i}
+ end
+end
+
+class Array
+ def frequencies
+ dictionary = {}
+ self.map {|i| dictionary.has_key?(i) ? dictionary[i]+=1 : dictionary[i]=1}
+ dictionary
+ end
+
+ def average
+ sum = 0.0
+ self.map {|i| sum += i}
+ (sum/self.size)
+ end
+
+ def drop_every n
+ new_list = []
+ self.map {|i| if (self.index(i)+1)%n != 0 then new_list << i end}
+ new_list.to_s
+ end
+
+ def after_n n
+ self[-(self.size+n)]
+ end
+
+ def combine_with other
+ result = []
+ smaller = [self.size,other.size].min
+ (0...smaller).map{|i| result << self[i] << other[i]}
+ result += self.drop(smaller) + other.drop(smaller)
+ result.to_s
+ end
+end