首頁 >後端開發 >Python教學 >Python實現取得前100組勾股數的方法

Python實現取得前100組勾股數的方法

不言
不言原創
2018-05-04 14:39:534611瀏覽

這篇文章主要介紹了Python實現獲取前100組勾股數的方法,涉及Python數值計算與判斷相關操作技巧,需要的朋友可以參考下

本文實例講述了Python實現獲取前100組勾股數的方法。分享給大家供大家參考,具體如下:

本來想採用窮舉試探的方式來做這個演算法,後來發現還是有點麻煩。從網路上找來了一個求解方法如下:

當a為大於1的奇數2n 1時,b=2n^2 2n,c=2n^2 2n 1。實際上就是把a的平方數拆成兩個連續自然數。

寫程式碼如下:

#!/usr/bin/python
for n in range(1,101):
 a = 2 * n +1
 b = 2 * (n** 2) + 2 * n
 c = b + 1
 # check theresult
 if a ** 2 +b ** 2 == c ** 2:
  print("a= %d, b = %d, c = %d" %(a,b,c))

程式執行結果:

a = 3, b = 4, c = 5
a = 5, b = 12, c = 13
a = 7, b = 24, c = 25
a = 9, b = 40, c = 41
a = 11, b = 60, c = 61
a = 13, b = 84, c = 85
a = 15, b = 112, c = 113
a = 17, b = 144, c = 145
a = 19, b = 180, c = 181
a = 21, b = 220, c = 221
a = 23, b = 264, c = 265
a = 25, b = 312, c = 313
a = 27, b = 364, c = 365
a = 29, b = 420, c = 421
a = 31, b = 480, c = 481
a = 33, b = 544, c = 545
a = 35, b = 612, c = 613
a = 37, b = 684, c = 685
a = 39, b = 760, c = 761
a = 41, b = 840, c = 841
a = 43, b = 924, c = 925
a = 45, b = 1012, c = 1013












































##. a = 47, b = 1104, c = 1105
a = 49, b = 1200, c = 1201
a = 51, b = 1300, c = 1301
a = 53, b = 1404, c = 1405
a = 55, b = 1512, c = 1513
a = 57, b = 1624, c = 1625
a = 59, b = 1740, c = 1741
a =#a = 59, b = 1740, c = 1741
a = 61, b = 1860, c = 1861
a = 63, b = 1984, c = 1985
a = 65, b = 2112, c = 2113
a = 67, b = 2244, c = 2245
a = 69, b = 2380, c = 2381
a = 71, b = 2520, c = 2521
a = 73, b = 2664, c = 2665
a = 75, b = 2812, c = 2813
a = 77, b = 2964, c = 2965
a = 79, b = 3120, c = 3121
a = 81, b = 3280, c = 3281
a = 83, b = 3444, c = 3445
a = 85, b = 3612, c = 3613
a = 87, b = 3784, c = 3785
a = 89, b = 3960, c = 3961
a = 91, b = 4140, c = 4141
a = 93, b = 4324, c = 4325
a = 95, b = 4512, c = 4513
a = 97, b = 4704, c = 4705
a = 99, b = 4900, c = 4901
a = 101, b = 5100, c = 5101
a = 103, b = 5304, c = 5305
a = 105, b = 5512, c = 5513
a = 107, b = 5724, c = 5725
a = 109, b = 5940, c = 5941
a = 109, b = 5940, c = 5941

a = 111, b = 6160, c = 6161
a = 113, b = 6384, c = 6385

a = 115, b = 6612, c = 6613

a = 117, b = 6844, c = 6613

a = 117, b = 6844, c = 6845

a = 119, b = 7080, c = 7081

a = 121, b = 7320, c = 7321a = 123, b = 7564, c = 7565##a = 123, b = 7564, c = 7565

a = 125, b = 7812, c = 7813

a = 127, b = 8064, c = 8065a = 129, b = 8320, c = 8321a = 131, b = 8580, c = 8581

a = 133, b = 8844, c = 8845

a = 135, b = 9112, c = 9113

a = 137, b = 9384, c = 9385

a = 139, b = 9660, c = 9661

a = 141, b = 9940, c = 9941

a = 143, b = 10224, c = 10225a = 145, b = 10512, c = 10512, c =# a = 147, b = 10804, c = 10805a = 149, b = 11100, c = 11101

a = 151, b = 11400, c = 11401###a = 151, b = 11400, c = 11401###a = 153, b = 111 c = 11705###a = 155, b = 12012, c = 12013###a = 157, b = 12324, c = 12325###a = 159, b = 12640, c = 12641##a = 159, b = 12640, c = 12641##a = 161, b = 12960, c = 12961###a = 163, b = 13284, c = 13285###a = 165, b = 13612, c = 13613###a = 167, b = 1394, c = 1394 = 167, b = 1394 = 13945###a = 169, b = 14280, c = 14281###a = 171, b = 14620, c = 14621###a = 173, b = 14964, c = 14621###a = 173, b = 14964, c = 14965##a = b = 15312, c = 15313###a = 177, b = 15664, c = 15665###a = 179, b = 16020, c = 16021###a = 181, b = 16380, 1638011 ##a = 183, b = 16744, c = 16745###a = 185, b = 17112, c = 17113###a = 187, b = 17484, c = 17485###a = 189, b = 189, b = 17485###a = 17860, c = 17861###a = 191, b = 18240, c = 18241###a = 193, b = 18624, c = 18625###a = 195, b = 18624, c = 18625###a = 195, b = 19012, c#19011 a = 197, b = 19404, c = 19405###a = 199, b = 19800, c = 19801###a = 201, b = 20200, c = 20201######################################################################################## 她中加入了是否為勾股數的判斷,因此這個清單應該是準確的。這個小題目解算下來,我自己的感覺是做事的方法中演算法還是至關重要的! ######相關推薦:#########Python實現的求解最大公約數演算法範例############基於matplotlib Python實作正弦訊號的時域波形和頻譜圖範例###############################

以上是Python實現取得前100組勾股數的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn