如果a+b+c = 1000, 且a^2 + b^2 = c^2(a,b,c为自然数),如何求出所有a, b, c可能的组合?
{
a+b+c=1000
a^2 + b^2 = c^2
}
import time # def condition_solution(): # start_time = time.time() # for a in range(1001): # for b in range(1001): # for c in range(1001): # if 1000 == a + b + c and a*a + b*b == c*c: # print("a, b, c: %d, %d, %d" % (a, b, c)) # end_time = time.time() # cost = end_time - start_time # pri# nt("cost: %f" % cost) # def condition_solution(): start_time = time.time() for a in range(1001): for b in range(1001): c = 1000 - a - b if 1000 == a + b + c and a*a + b*b == c*c: print("a, b, c: %d, %d, %d" % (a, b, c)) end_time = time.time() cost = end_time - start_time print("cost: %f" % cost) if __name__ == "__main__": condition_solution()
原文:https://www.cnblogs.com/1314520xh/p/11531076.html