Skip to content

Instantly share code, notes, and snippets.

@kopylovvlad
Created April 17, 2016 20:21
Show Gist options
  • Save kopylovvlad/9d79a936a1c05c329774152a02ab8e95 to your computer and use it in GitHub Desktop.
Save kopylovvlad/9d79a936a1c05c329774152a02ab8e95 to your computer and use it in GitHub Desktop.
require 'prime'
def divisor(number)
answer = []
i = number
while i > 0
if number % i == 0
answer.push( i )
end
i -= 1
end
answer
end
def divisor_by_factorization(number)
primes, powers = number.prime_division.transpose
exponents = powers.map{|i| (0..i).to_a}
divisors = exponents.shift.product(*exponents).map do |powers|
primes.zip(powers).map{|prime, power| prime ** power}.inject(:*)
end
divisors.sort
end
begin_time = Time.now
for num in 10..25000
divisor( num )
end
puts Time.now - begin_time
begin_time = Time.now
for num in 10..25000
divisor_by_factorization( num )
end
puts Time.now - begin_time
#18
#0.9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment