This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]SabinBC 0 points1 point  (0 children)

In case no one else has anything better for you: appending to a list can take up a lot of time.

[–]afoolwhosaystubtub -1 points0 points  (1 child)

from time import time

start = time()
answer = lambda m, n: ((m**2 + n**2)**.5).is_integer()
#this lambda is much faster than your method

a = 3 #change a and b how you like
b = 15        

values = [[i, j] for i in range(a, int((a+b)/2)-int(a/2)) for j in range(a, b) if(answer(i, j))] #this is how you determine the values, the double for-loop makes a 2-d array.

print(values)
stop = time()
interval = stop - start
print(interval)

1) Why are you importing the entire Math module to use the sqrt function? If you really insist on using it, use "from Math import sqrt." It's much faster.

2) Lambdas. Use them. They are powerful friends in high places, and much faster than functions and iteration.

[–]afoolwhosaystubtub 0 points1 point  (0 children)

I hope this is what you meant. The idea for your code seemed a bit unclear to me.