Write a program triples_2.py that finds all triples of consecutive positive three-digit integers each of which is the sum of two squares, that is, all triples of the form (n,n+1,n+2) such that:
n, n+1 and n+2 are integers at least equal to 100 and at most equal to 999;
each of n, n+1 and n+2 is of the form a^2 + b^2
.
Hint: As we are not constrained by memory space for this problem, we might use a list that stores an integer for all indexes n in [100,999], equal to 1 in case n is the sum of two squares, and to 0 otherwise. Then it is just a matter of finding three consecutive 1's in the list. This idea can be refined (by not storing 1s, but suitable nonzero values) to not only know that some number is of the form a^2 + b^2, but also know such a pair (a,b)...The sample output is indicative only when it comes to the decompositions into sums of squares, as such decompositions are not unique.
If you are stuck, but only when you are stuck, then use triple_2_scaffold.py.
# 我的解法
#寫的有點久了誊册,邊界是考慮一下?算一算得出的
#有一個問題是題目要求n片迅,n+1满葛,n+2只是一個平方數(shù)和,但一個數(shù)可能有很多平方數(shù)和組合送粱,最終又有打印要求,只能當作鍛煉找找規(guī)律寫一寫,不然這種打印平方和的數(shù)的規(guī)律也應該是在題目中有所講明的构拳,這里是更靠近的兩個數(shù)的平方和,所以更新的時候想著一方從大到小梁棠,另一方從小到大置森,邊界可算可試,讓他們在刷新過程中聚攏到目的值
# Finds all triples of consecutive positive three-digit integers
# each of which is the sum of two squares.
sums_of_two_squares = [None] * 1_000
def nb_of_consecutive_squares():
L=[]
for i in range(31,7,-1):#31是雙位數(shù)得三位數(shù)平方得最大值
for j in range(0,24):
x=i*i+j*j
if x<999 and j<=i:
sums_of_two_squares[x]=f'{j}^2+{i}^2'
#else:
#break
for i in range(100,998):
if sums_of_two_squares[i]!=None and sums_of_two_squares[i+1]!=None and sums_of_two_squares[i+2]!=None:
L.append((i,i+1,i+2))
L.sort()
#print(L)
for i in range(len(L)):
print(f'{L[i]} (equal to ({sums_of_two_squares[L[i][0]]}, {sums_of_two_squares[L[i][1]]}, {sums_of_two_squares[L[i][2]]})) is a solution.')
nb_of_consecutive_squares()
#馬丁解法
#Finds all triples of consecutive positive three-digit integers each of which is the sum of two squares.
def nb_of_consecutive_squares(n):#None返回false符糊,有值返回true凫海,not取反
if not sums_of_two_squares[n]:
return 0
if not sums_of_two_squares[n + 1]:
return 1
if not sums_of_two_squares[n + 2]:
return 2
return 3
# The largest number whose square is a 3-digit number.
max = 31 #馬丁的程序是規(guī)規(guī)矩矩,習慣太好男娄,可讀性代碼行贪,包括命名規(guī)范,10句縮一句~
# For all n in [100, 999], if n is found to be of the form a^2 + b^2
# then sums_of_two_squares[n] will be set to (a, b).
# Note that there might be other decompositions of n into a sum of 2 squares;
# we just recall the first decomposition we find.
# Also note that we waste the 100 first elements of the array;
# we can afford it and this choice makes the program simpler.
sums_of_two_squares = [None] * 1_000
for i in range(max + 1):#這種解法其實思路更簡單模闲,不斷更新中也是聚攏到相近值了
for j in range(i, max + 1):
n = i * i + j * j
if n < 100:
continue
if n >= 1_000:
break
sums_of_two_squares[n] = i, j
for n in range(100, 1_000):
i = nb_of_consecutive_squares(n)
if i < 3:
# There is no potential triple before n + i + 1;
# the loop will increment n by 1.
n += i
continue
print(f'({n}, {n + 1}, {n + 2}) (equal to ('
f'{sums_of_two_squares[n][0]}^2+{sums_of_two_squares[n][1]}^2, '
f'{sums_of_two_squares[n + 1][0]}^2+{sums_of_two_squares[n + 1][1]}^2, '
f'{sums_of_two_squares[n + 2][0]}^2+{sums_of_two_squares[n + 2][1]}^2'
')) is a solution.'
)
# We assume we could have two solutions of the form
# (n, n + 1, n + 2) and (n + 1, n + 2, n + 3)
# (but as the solution shows, this never happens...),
# hence n is incremented by only 1 in the next iteration of the loop.
# We could avoid checking that sums_of_two_squares[n + 1] and
# sums_of_two_squares[n + 2] are not equal to 0, but why make the program
# more complicated for no significant gain?