Optimizing Project Euler #12 (Python 3) I just figured out Project Euler problem #12. I'm still really new to Python, so I'm putting my code up here in the hopes that someone will be able to give me some tips for optimizing my code to run faster. It took an incredibly long time to run, so I'm hoping that someone will be able to give me pointers.

If anyone is willing, would you mind looking over my solution and letting me know how I can improve on it, either through optimization or style?

The link to the problem is here: Project Euler #12

run = True
p = 2
numberInUse = 3

def checkDivisors(number):
    numberOfDivisors = 0
    for x in range(1, int(number+1)):
        if number%x ==0:
            numberOfDivisors += 1
    return(numberOfDivisors)

while run == True: 
    if checkDivisors(numberInUse) &gt; 500:
        run = False
        print(numberInUse, "is the answer!")
    else:
        p += 1
        numberInUse += p
Project Euler problems are more about math than programming.

You must prove (or at least observe) the following:


An nth triangular number is \$\dfrac{n(n+1)}{2}\$. One of the numerator's factors is obviously even.
The number_of_divisors is a multiplicative function (known as \$\sigma\$), meaning that if \$m\$ and \$n\$ are coprime, then \$\sigma(mn) = \sigma(m)\cdot\sigma(n)\$
\$n\$ and \$n+1\$ are necessarily coprime.
Once you computed \$\sigma(n)\$ and \$\sigma(n+1)\$ on a certain iteration, on the next iteration you'd only need to compute \$\sigma(n+2)\$. Using memoization, you may even avoid this when \$n+2\$ is even.


This should be enough to build an efficient solution.