Иван обнови решението на 16.10.2013 12:19 (преди над 11 години)
+class Integer
+ # The function 'prime?' can be shorter
+ # but readability is prior to brevity
+ # the 'return's fix a problem with Ruby reading the lines
+ # as `if <condition> <do this>`
+ # Also: comments are in english because `skeptic` doesn't like cyrillic
+ def prime?
+ return false if self < 2
+ return true if self == 2
+ upper_cap = Math.sqrt(self).to_i + 1
+ search_range = (2..upper_cap)
+ search_range.each {|x| return false if (self % x) == 0 and self != 2}
+ true
+ end
+
+ def prime_factors
+ list = []
+ (2..self.abs).each {|x| list << x if x.prime? and (self.abs % x == 0)}
+ if list.reduce(:*) == self.abs
+ list.sort
+ else
+ list << (self.abs / list.reduce(:*)).prime_factors
+ list.flatten.sort
+ end
+ end
+
+
+ def digits
+ self.abs.to_s.split("").map {|x| x.to_i}
+ end
+
+ def harmonic
+ abs_self = self.abs.to_r + 1
+ harmonic_result = 0.to_r
+ harmonic_result += (1 / (abs_self -= 1)) while abs_self != 1
+ harmonic_result
+ end
+end