Станислав обнови решението на 16.10.2013 10:36 (преди около 11 години)
+class Integer
+ def prime?
+ return false if abs < 2
+ (2..abs/2).each { |n| return false if abs % n == 0 }
+ true
+ end
+
+ def prime_factors
+ factors = []
+ (2..abs).each { |n| factors.concat(single_prime_divisors(abs, n)) }
+ factors
+ end
+
+ def single_prime_divisors(number, divisor)
+ divisors = []
+ while number % divisor == 0 and divisor.prime?
+ divisors << divisor
+ number /= divisor
+ end
+ divisors
+ end
+
+ def harmonic
+ harmonic_number, number = Rational(1), abs
+ while number > 1
+ harmonic_number += Rational(1, number)
+ number -= 1
+ end
+ harmonic_number
+ end
+
+ def digits
+ abs.to_s.split('').map { |digit| digit.to_i}
+ end
+end
+
+class Array
+ def average
+ sum = 0
+ each { |n| sum = sum + n }
+ sum / length.to_f
+ end
+
+ def frequencies
+ hash = Hash.new(0) # NoMethodError for + if hash = {}
+ each { |item| hash[item] += 1 }
+ hash
+ end
+
+ def drop_every(n)
+ reverse_each do |item|
+ delete_at(index(item)) if (index(item) + 1) % n == 0
+ end
+ end
+
+ def combine_with(other)
+ return other if empty?
+ zip(other).flatten.delete_if { |item| item.nil? }
+ end
+end