using System; using System.Collections.Generic; using System.Text; namespace Euler12 { //What is the value of the first triangle number to have over five hundred divisors? class Program { static public int factors(long lInput) { //List liFac = new List(); int ans = 0; //for (long i = lInput; i > (long)Math.Sqrt(lInput); --i) for (long i = 1; i <= (long)Math.Sqrt(lInput); ++i) { double div = (double)lInput / (double)i; if (div == (long)(div)) { //liFac.Add(i); //if (!liFac.Contains((long)div)) liFac.Add((long)div); ans += 2; } } return ans; //liFac.Count; } static void Main(string[] args) { //Sthg like 14 hours of computation... //Might be better to go the prime factor way //http://mathschallenge.net/index.php?section=faq&ref=number/number_of_divisors //Or not: drop the list, use an integer as result storage, and here it is: 30 sec int i = 1; int curMil = 1; Console.WriteLine(DateTime.Now + " Search..."); long lFactors = 0; long maxFac = 0; while (lFactors < 500) { long lTri = (long) i*(i+1)/2; if(lTri > curMil * 1000000) { Console.WriteLine(DateTime.Now + ": " + curMil + " million explored with iteration :" + i); ++curMil; } lFactors = factors(lTri); if (lFactors > maxFac) { Console.WriteLine("Current iteration: " + i + " (Tri= " + lTri + ") and maxFac is now: " + lFactors); maxFac = lFactors; } ++i; } Console.ReadLine(); } } }