0

I wrote a program in python for Project Euler Problem 12. This code takes triangle numbers and divides them to find the smallest triangle number that can be divided 500 times. My code works, but is painfully slow and takes an unreasonable amount of time to yield the answer. Are there small improvements I could make that would improve performance or do I need to start over completely with a different approach? thanks!

# Project Euler Problem #12 
# Highly divisible Triangle Number
# What is the value of the first triangle number to have over five hundred divisors?


# Variables
add = 0
i = 0
divisors = [0]

def multiple_counter(n):
    multiples = []
    for i in range(1,n+1):
        if n % i == 0:
            multiples.append(i)
    return len(multiples)



while max(set(divisors)) <= 499:

   i += 1
    add += i
   divisors = []
   divisors.append(multiple_counter(add))
   print( max(set(divisors)),  add, i) # this is my debugger

print (add)
  • Welcome to SO. Wouldn't it be simpler just to use ```divisor``` as a value instead of a list since you're resetting it in the while loop anyway so it'll always be just one value. or did I read it wrong? – ewokx Oct 09 '20 at 02:44
  • thanks! Yes so I only added the reset of the list to check if there was a problem, the actual function that checks divisors returns a number(the number of multiples). which is then checked by the while loop. – Viper89 Oct 09 '20 at 02:49

0 Answers0