Йордан обнови решението на 14.10.2013 22:06 (преди около 11 години)
+class Integer
+ def prime?
+ max=Math.sqrt((self).abs).floor
+ max.downto(2) do |n| if self % n == 0 then return false end end
+ return true
+ end
+
+ def prime_factors
+ array_prime , new_numb , delitel = Array.new , self.abs , 2
+ while new_numb > 1
+ if delitel.prime? and new_numb % delitel==0 then array_prime << delitel
+ new_numb /= delitel
+ else delitel = delitel.next
+ end
+ end
+ return array_prime.sort
+ end
+
+ def harmonic
+ sum=Rational(1,1)
+ 2.upto(self) do |n| sum += Rational(1.0,n)
+ end
+ return sum
+ end
+
+ def digits
+ array_digits , length=Array.new , Math.log(self.abs,10).ceil+1
+ str = self.abs.to_s
+ length.times do |i|
+ if str[i] != nil then array_digits <<str[i].to_i
+ else
+ return array_digits end
+ end
+ end
+end
+class Array
+ def frequencies
+ br, h=0 , Hash.new
+ self.each do |i| if h[i] == nil then h[i]=1 else h[i]=h[i].to_i.next end
+ end
+ return h
+ end
+ def average
+ sum=0.0
+ self.each do |i| sum += i
+ end
+ if self.length !=0 then return sum/self.length else return "Masiva e prazen"
+ end
+ end
+ def drop_every( x )
+ new_array=Array.new
+ 1.upto(self.length) do |i| if i % x !=0 then new_array << self[i-1] end
+ end
+ return new_array
+ end
+ def combine_with( other )
+ maxelem , com_arr=[ self.length , other.length ].max,Array.new
+ 0.upto( maxelem ) do |i| if self[i] != nil then com_arr << self[i] end
+ if other[i] != nil then com_arr << other[i] end
+ end
+ return com_arr
+ end
+end