# This script finds the smallest triangle number that has at least N divisors, where N is the input to the script

# This function calculates the exponent r where p^r is a divisor of n, i.e. n % p^r == 0
def expo(n,p)
  r = n % p
  exp = 0
  while r == 0
    exp += 1
    r = n % (p ** (exp+1))
  end
  return exp
end

# This returns a prime factorization of an integer as a hash
# The keys of the hash are primes and the values are their exponents
def factor(n)
  factors = Hash.new
  s = n * 0.5
  p = (2..s).to_a
  p.each{|num|
    if num
      r = expo(n,num)
      if r
        factors[num] = r
      end
      val = num*2
      while val <=s
        p[val - 2] = nil
        val += num
      end
    end
  }
  return factors
end

# This calculates the number of divisors of a given integer
def numDivisors(n)
  total = 1
  factor(n).each_value{|num|
    total *= (num+1)
  }
  return total
end

t = Time.new
n = 1
num = 1
max = ARGV[0].to_i
while num <= max
  triangle = n*(n+1)/2
  num = numDivisors(triangle)
  n += 1
end
puts Time.new - t
puts n*(n-1)/2