# # # # # # # # #
# /30/euler-012.rb
#
# by               Jan Lelis
# e-mail:          mail@janlelis.de
# type/version:    ruby 1.9
# snippet url:     http://rbJL.net/30/euler-012.rb
# original post:   http://rbJL.net/30-project-euler-6-7-8-9-10-11-12-13-14-15-16-17-18-ruby
# license:         CC-BY-SA (DE)
#
# (c) 2010 Jan Lelis.

# http://projecteuler.net/index.php?section=problems&id=12
#  the first triangle number (1+2+3+4+...) which has over five hundred divisors

# you can calculate the number of factors with this formula:
#  (a_1 + 1) * (a_2 + 1) * (a_3 + 1)
# where a_n are the exponents of the prime factors:
#  (p_1**a_1) * (p_2**a_2) * (p_3**a_3)

require 'mathn'

MAX = 500

triangle = current = 1

until MAX < Prime.prime_division(triangle).inject(1){ |product, (_, factor)|
  product * (factor + 1)
}
  current  += 1
  triangle += current
end

puts triangle