python馬丁challenge10.Finding particular sequences of triples of the form (n, n+1, n+2)

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?
屏幕快照 2019-04-03 上午11.37.26.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末建瘫,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子围橡,更是在濱河造成了極大的恐慌暖混,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件翁授,死亡現(xiàn)場離奇詭異拣播,居然都是意外死亡,警方通過查閱死者的電腦和手機收擦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門贮配,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人塞赂,你說我怎么就攤上這事泪勒。” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵圆存,是天一觀的道長叼旋。 經(jīng)常有香客問我,道長沦辙,這世上最難降的妖魔是什么夫植? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮油讯,結(jié)果婚禮上详民,老公的妹妹穿的比我還像新娘。我一直安慰自己陌兑,他們只是感情好沈跨,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著兔综,像睡著了一般饿凛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上邻奠,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天笤喳,我揣著相機與錄音为居,去河邊找鬼碌宴。 笑死,一個胖子當著我的面吹牛蒙畴,可吹牛的內(nèi)容都是我干的贰镣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼膳凝,長吁一口氣:“原來是場噩夢啊……” “哼碑隆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蹬音,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤上煤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后著淆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體劫狠,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年永部,在試婚紗的時候發(fā)現(xiàn)自己被綠了独泞。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡苔埋,死狀恐怖懦砂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤荞膘,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布罚随,位于F島的核電站,受9級特大地震影響羽资,放射性物質(zhì)發(fā)生泄漏毫炉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一削罩、第九天 我趴在偏房一處隱蔽的房頂上張望瞄勾。 院中可真熱鬧,春花似錦弥激、人聲如沸进陡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽趾疚。三九已至,卻和暖如春以蕴,著一層夾襖步出監(jiān)牢的瞬間糙麦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工丛肮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留赡磅,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓宝与,卻偏偏與公主長得像焚廊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子习劫,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354