Created
May 27, 2011 22:58
-
-
Save johana-star/996357 to your computer and use it in GitHub Desktop.
Euler Project Problem #012
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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