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.
Some Python functions around this question. It uses [Counters](https://docs.python.org/2/library/collections.html#collections.Counter), the perfect 
high level tool to do that. Hope this can help you to find the keys enhancements.

```
from collections import Counter  

def factors(n):
    divisor=2
    while divisor*divisor<=n:
        if n%divisor==0:
            return Counter({divisor:1})+factors(n//divisor)
        divisor += 1
    return Counter({n:1}) # prime number

def nbdiv(factors):
    p=1
    for factor in factors : p *= factors[factor]+1
    return p 

def nbdivtri(n):
    a,b=n,n+1
    if a%2==0 : a//=2
    else: b//=2
    return nbdiv(factors(a)+factors(b))  

```