Георги обнови решението на 14.10.2013 23:55 (преди около 11 години)
+class Integer
+ def prime?
+ return false if self < 0
+ (2..self/2).each do |factor|
+ return false if self%factor==0
+ end
+ return true
+ end
+
+ def prime_factors(num=self.abs)
+ list =[]
+ factor=2
+ while num>1
+ list.push factor while num>num =factor?(num,factor)
+ factor+=1
+ end
+ return list
+ end
+
+ def factor? (num,factor)
+ if num%factor==0
+ return num/factor
+ else
+ return num
+ end
+ end
+
+ def harmonic
+ result=0
+ (1..self).each { |num| result+=Rational(1,num) }
+ return result
+ end
+
+ def digits
+ num=self.abs
+ list = []
+ while num > 0
+ list.unshift num%10
+ num /=10
+ end
+ return list
+ end
+end
+
+class Array
+ def frequencies
+ list= Hash.new(0)
+ self.each { |element| list[element]+=1 }
+ return list
+ end
+
+ def average
+ average=0.0
+ self.each { |element| average+=element}
+ average /=self.size
+ return average
+ end
+ def drop_every(n)
+ index=n-1
+ list=self
+ while index<list.size
+ list.delete_at(index)
+ index +=n-1
+ end
+ return list
+ end
+
+ def combine_arrays(first, second)
+ list=[]
+ 0.upto(first.size>second.size ? first.size-1 : second.size-1).each do|index|
+ list.push(first[index]) if first[index]
+ list.push(second[index]) if second[index]
+ end
+ return list
+ end
+
+ def combine_with(other)
+ list=[]
+ if self.empty? or other.empty?
+ list=self+other
+ else
+ list=combine_arrays(self, other)
+ end
+ return list
+ end
+end