Question: I'm struggling for about 3 hours for now to fasten my Project Euler #12 code. I achieved to save a few seconds for the case that numbers has over 130 divisors, my first program went to 2.33 seconds now it does it in 1.169 seconds. However I never had the patience to wait for the 500 divisors number. How can I fasten my code? I tried the case to get the divisors from n and n+1 but it just slowed my program even more... Here is my code.

```
static bool isPrime(int num) {
        if (num % 2 == 0 && num != 2)
            return false;
        else
            for (int i = num; i < Math.Sqrt(num) + 1; i++) {
                if (num % i == 0)
                    return false;
            }
        return true;
    }

    static void Main(string[] args) {

        Stopwatch time = new Stopwatch();
        time.Start();

        int trianglenumber = 0;
        int divizori = 0;

        for (int i = 3; i < Int32.MaxValue; i+=2) {
            if (isPrime(i) != false) {
                int tempnumber = 0;
                tempnumber = (i * (1 + i)) / 2;
                for (int k = 1; k < tempnumber + 1; k++) {
                    if (tempnumber % k == 0) {
                        divizori++;
                    }
                }

                if (500 < divizori) {
                    trianglenumber = tempnumber;
                    break;
                }
                divizori = 0;
            }
        }
            time.Stop();
            double timp = time.ElapsedMilliseconds ;

            Console.WriteLine(trianglenumber);
            Console.Write("Runtime: " + timp/1000 + " seconds");
            Console.ReadKey();
    } 

```

Answer: 
The **big** slow down is in your isPrime() method.

Take a look at counting factors by prime factorization. You'll see that counting 500 factors is MUCH quicker this way.

<http://www.mathsisfun.com/prime-factorization.html>

Also, how you're getting your triangular numbers should be much simpler.

```
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

```

Notice the difference between each sequential set of numbers....

1 + **2** = 3

3 + **3** = 6

6 + **4** = 10

10+ **5** = 15

15+ **6** = 21

and so on...take a look at that middle number.

Just for reference, my version of this using the prime factorization method takes roughly 6ms to find the answer....pretty quick.
I solved this question using a Greedy algorithm less than 0.187 ms.

```
    static void Main(string[] args)
    {
        // 76576500
        long age = 10000;
        double alpha = 1.5;
        double betta = 0.1;
        Random ran = new Random();
        long mmm = long.MaxValue;
        double RR = 0;
        var a = DateTime.Now;
        for (int i = 0; i < 5000; i++)
        {
            long N = age * (age + 1) / 2;
            long count = 0;
            for (int j = 2; N!=1; j++)
            {
                int c = 0;
                while (N % j == 0)
                {
                    N /= j;
                    c++;
                }
                count = (count + 1) * (c + 1) - 1;
            }
            double r = ran.Next() % 11;
            r = 1 / (Math.Pow(r, alpha) + 1);
            long R = (long)r + 1;
            if (count >= 500)
            {
                alpha *= 1 + 0.001;
                RR = RR * betta - (1-betta) * R;
                N = age * (age + 1) / 2;
                if (N < mmm)
                {
                    mmm = N;
                    Console.WriteLine(mmm);
                }
            }
            else
            {
                alpha *= 1 - 0.001;
                RR = RR * betta + (1 - betta) * R;
            }
            age += (long)RR;
        }
        var b = DateTime.Now - a;
        Console.WriteLine("R=" + mmm + "  " + b);
        string sss = Console.ReadLine();
    }

```