Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created May 27, 2011 22:58
Show Gist options
  • Save johana-star/996357 to your computer and use it in GitHub Desktop.
Save johana-star/996357 to your computer and use it in GitHub Desktop.
Euler Project Problem #012
# Solution to Project Euler's twelfth problem
# http://projecteuler.net/index.php?section=problems&id=12
# Find the first triangular number with more than 500 divisors.
def generate_triangle_numbers(ceiling=15000)
numbers = Array.new
sum = 0
(1..ceiling).each do |n|
sum = sum + n
if Math.sqrt(sum) > 8000 then numbers.push(sum) end
end
return numbers
end
def find_number_of_divisors(number)
ceiling = Math.sqrt(number).floor
count = 0
ceiling.downto(1) {|div| if number % div == 0 then count += 2 end}
if count > 500 then puts "#{number} has #{count} divisors." end
return count
end
a = Time.new
numbers = generate_triangle_numbers
numbers.each {|n| find_number_of_divisors(n)}
b = Time.new - a
p b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment