Иван обнови решението на 15.10.2013 02:44 (преди около 11 години)
+class Integer
+ def prime?
+ gcds = []
+ is_prime = false
+ (2..self+1).each{ |number| gcds << self.gcd(number) }
+ if gcds.uniq.length == 2 then is_prime = true end
+ return is_prime
+ end
+
+ def prime_factors
+ if self < 0 then number = -self else number = self end
+ pos = 2
+ factors = []
+ while pos <= number
+ number, pos, factors = self.prime_factors_help(number, pos, factors)
+ end
+ return factors
+ end
+
+ def prime_factors_help(number, pos, factors)
+ if number%pos == 0
+ factors << pos
+ number /= pos
+ pos = 2
+ else
+ pos += 1
+ end
+ return number, pos, factors
+ end
+
+ def harmonic
+ #if self < 0 then number = -self else number = self end
+ k, sum = 1, 0
+ while k <= self
+ sum += Rational(1, k)
+ k += 1
+ end
+ return sum.to_r
+ end
+
+ def digits
+ if self < 0 then number = -self else number = self end
+ degree = number.to_s.split(//)
+ digits_list = []
+ degree.each{ |digit| digits_list << digit.to_i }
+ digits_list
+ end
+end
+
+
+class Array
+ def frequencies
+ result = {}
+ self.each{ |n| if result[n] then result[n] += 1 else result[n] = 1 end }
+ result
+ end
+
+ def average
+ sum = 0
+ length = self.each{ |element| sum += element }.length
+ average_number = sum.to_f / length.to_f
+ end
+
+ def drop_every(position)
+ new_list = []
+ position_to_deleat = position - 1
+ while self.length >= position_to_deleat
+ new_list << self[position_to_deleat]
+ position_to_deleat += position
+ end
+ self - new_list
+ end
+
+ def combine_with(other)
+ i, combined = 0, []
+ while i < [self.length, other.length].min
+ combined << self[i] << other[i]
+ i += 1
+ end
+ if self[i] then larger_list = self else larger_list = other end
+ combined + larger_list[i..larger_list.length-1]
+ end
+end