Давид обнови решението на 16.10.2013 01:25 (преди около 11 години)
+class Integer
+ def prime?
+ 2.upto(Math.sqrt self.abs ).all? { |x| self.remainder(x).nonzero? }
+ end
+
+ def prime_factors n=self.abs, divisor=2, factors=[]
+ if n==1
+ factors
+ elsif n.remainder(divisor).zero?
+ prime_factors n/divisor, divisor, factors.push(divisor)
+ else
+ prime_factors n, divisor+1, factors
+ end
+ end
+
+ def harmonic
+ sum=Rational(0,1)
+ 1.upto(self).each {|x| sum+=Rational(1,x)}
+ sum
+ end
+
+ def digits
+ digits_array=[]
+ self.abs.to_s.each_codepoint {|x| digits_array.push(x-48)}
+ digits_array
+ end
+
+end
+
+class Array
+ def frequencies
+ frequency={}
+ self.each {|x| frequency.has_key?(x) ? frequency[x]+=1 : frequency[x]=1}
+ frequency
+ end
+
+ def average
+ self.inject {|a,b| a+b}/self.length.to_f
+ end
+
+ def drop_every(n)
+ reduced=[]
+ self.each_index {|x| reduced.push(self[x]) if (x+1).remainder(n).nonzero?}
+ reduced
+ end
+
+ def combine_with(arr)
+ combined_array=Array.new(self.length+arr.length)
+ self.each_index {|x| combined_array[2*x]=self[x]}
+ arr.each_index {|x| combined_array[2*x+1]=arr[x]}
+ combined_array.compact
+ end
+
+end