I am a newbie in the coding world. I was trying to solve the 12th problem of Project Euler to find first triangle number to have 500 divisors.
My code returns result in around 17s. I am sure there are ways to run it in a lot shorter time. Can anyone help in optimizing my code?
Here is my code:
def divisor_triangle_numbers(div):
tr_num = 1
n = 2
while True:
tr_num += n
divs = 0
for i in range(1,int(tr_num**0.5)+1):
if tr_num%i == 0:
divs += 2
if divs > div:
print(tr_num, divs)
return tr_num, divs
n += 1
You can try multithreading. It's not really optimisation but as it's fairly simple in python it may be a good easy improvement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.