I recently started going through the problems over at [ProjectEuler](projecteuler.net), and I wrote some code for one of the problems-9- that I think is decent?
def mainline():
for a in range(0,999+1):
for b in range(0,999+1):
c = (-a-b)+1000
if a*a+b*b==c*c and a+b+c == 1000 and a*b*c != 0:
return(a,b,c,(a*b*c))
print(mainline())
I can’t really think of any way to optimize it besides shifting about the order of the conditionals in the if statement to have the broadest checks run first? It runs at around ~60-70ms on my machine.
Any advice/tips would be appreciated.
PS:I am pretty new to python so don’t hold back, I have a lot to learn lol.
I can theoretically make the for loops run in steps of 5, which makes it near instantaneous, but it is also cheating as without knowing the answer I would not know they are all multiples of 5.
I also tried counting backwards from 1,000 down to 0 instead of from 0 up, and it made no difference.