Python經典練習題
網上能夠搜得到的答案為:
for i in range(1,85): if 168 % i == 0: j = 168 / i; if i > j and (i + j) % 2 == 0 and (i - j) % 2 == 0 : m = (i + j) / 2 n = (i - j) / 2 x = n * n - 100 print(x)
輸出答案為:
-99 21 261 1581
但其實四個數字,均不符合+100和+168后,仍為完全平方數的條件;
正確代碼如下:
import math n = 0 count = 0 while True: first = n + 100 second = n + 168 first_sqrt = int(math.sqrt(first)) second_sqrt = int(math.sqrt(second)) if (first_sqrt*first_sqrt == first) and (second_sqrt*second_sqrt == second): print(n) break n = n + 1
正確答案只有一個:
156