Моника обнови решението на 13.10.2013 18:00 (преди около 11 години)
+class Integer
+
+ def prime?
+ return false if self == 0 or self == 1 or self < 0
+
+ middle = Math.sqrt(self)
+ (2..middle).each do |d|
+ return false if self % d == 0
+ end
+
+ return true
+ end
+
+ def primeFactor? f
+ f.prime? and self % f == 0
+ end
+
+ def prime_factors
+ factors = []
+ self > 0 ? number = self : number = -self
+
+ while number > 1
+ (2..number).any? do |f|
+ number.primeFactor? f and factors << f and number /= f
+ end
+ end
+
+ factors.sort!
+ end
+
+ def harmonic
+ sum = 0
+ (1..self).each do |n|
+ sum += Rational(1, n)
+ end
+ sum
+ end
+
+ def digits
+ self > 0 ? number = self : number = -self
+ digits = []
+ while number > 0
+ digits << number % 10 and number /= 10
+ end
+ digits.reverse!
+ end
+
+end
+
+class Array
+
+ def frequencies
+ freqs = {}
+ (0..self.length-1).each do |i|
+ if freqs.key?(self[i])
+ freqs[self[i]] += 1
+ else
+ freqs.store(self[i], 1)
+ end
+ end
+ freqs
+ end
+
+ def average
+ return if self.empty?
+ sum = 0.0
+ count = 0
+ (0..self.length-1).each do |i|
+ sum = sum + self[i].to_i and count = count + 1
+ end
+ result = sum / count
+ end
+
+ def drop_every(n)
+ array = []
+ (0..self.length-1).each do |i|
+ array << self[i - 1] unless i % n == 0
+ end
+ array
+ end
+
+ def combine_with(other)
+ return self if other.empty?
+ return other if self.empty?
+ array = []
+ (0..[self.length-1, other.length-1].max).each do |i|
+ (array << self[i] if self[i]) and (array << other[i] if other[i])
+ end
+ array
+ end
+
+end