Skip to content

Instantly share code, notes, and snippets.

@thumblemonks
Created February 27, 2010 04:01
Show Gist options
  • Save thumblemonks/316466 to your computer and use it in GitHub Desktop.
Save thumblemonks/316466 to your computer and use it in GitHub Desktop.
# See: http://projecteuler.net/index.php?section=problems&id=12
#
# We don't need to actually store the individual factors so long as we know how many were identified
def divisors(limit)
triangle, triangle_sum, found_divisors = 0, 0, 0
while found_divisors < limit
triangle_sum += (triangle += 1)
next unless triangle_sum % 2 == 0 # Only caring about even numbers
found_divisors = 2 # default = [1, triangle_sum]
position, last_max_factor = 2, triangle_sum
while position < last_max_factor
if triangle_sum % position == 0
last_max_factor = triangle_sum / position
found_divisors += 2
end
position += 1
end
end
triangle_sum
end
puts divisors(ARGV[0].to_i)
gus@jaknowlden:trinumbers> time ruby1.9 projecteuler-trianglenumber.rb 500
76576500
real 0m2.930s
user 0m2.899s
sys 0m0.015s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment