本文实例讲述了Python实现获取前100组勾股数的方法。分享给大家供大家参考,具体如下:
本来想采用穷举试探的方式来做这个算法,后来发现还是有点麻烦。从网络上找来了一种求解方法如下:
当一个为大于1的奇数2 n + 1时,b=2 n ^ 2 + 2 n, n c=2 ^ 2 + 2 n + 1。实际上就是把一个的平方数拆成两个连续自然数。
编写代码如下:
# !/usr/bin/python n的范围(1101):=2 * n + 1 b=2 * (n * * 2) + 2 * n c=b + 1 #检查结果 如果一个* * 2 + b * * 2==c * * 2: 打印(“=% d、b=% d c=% d % (a, b, c)) >之前程序执行结果:
=3, b=4, c=5
引用>
c==5, b=12日13日
c==7, b=24日25日
=9,b=40, c=41
=11,b=60, c=61
=13,b=84, c=85
=15,b=112, c=113
=17,b=144, c=145
=19,b=180, c=181
=21,b=220, c=221
=23日,b=264, c=265
=25,b=312, c=313
=27,b=364, c=365
a=29, b=420, c=421
=31,b=480, c=481
a=33=544, c=545
=35,b=612, c=613
=37,b=684, c=685
a=39 b=760, c=761
a=41, b=840, c=841
=43,b=924, c=925
=45,b=1012, c=1013
a=47, b=1104, c=1105
a=49, b=1200, c=1201
a=51, b=1300, c=1301
=53,b=1404, c=1405
a=55, b=1512, c=1513
a=57, b=1624, c=1625
a=59, b=1740, c=1741
=61,b=1860, c=1861
=63,b=1984, c=1985
=65,b=2112, c=2113
=67,b=2244, c=2245
=69,b=2380, c=2381
=71,b=2520, c=2521
=73,b=2664, c=2665
=75,b=2812, c=2813
=77,b=2964, c=2965
=79,b=3120, c=3121
=81,b=3280, c=3281
=83,b=3444, c=3445
=85,b=3612, c=3613
=87,b=3784, c=3785
=89,b=3960, c=3961
=91,b=4140, c=4141
=93,b=4324, c=4325
=95,b=4512, c=4513
=97,b=4704, c=4705
=99,b=4900, c=4901
=101,b=5100, c=5101
=103,b=5304, c=5305
=105,b=5512, c=5513
=107,b=5724, c=5725
=109,b=5940, c=5941
=111,b=6160, c=6161
=113,b=6384, c=6385
=115,b=6612, c=6613
=117,b=6844, c=6845
=119,b=7080, c=7081
=121,b=7320, c=7321
=123,b=7564, c=7565
=125,b=7812, c=7813
=127,b=8064, c=8065
=129,b=8320, c=8321
=131,b=8580, c=8581
=133,b=8844, c=8845
=135,b=9112, c=9113
=137,b=9384, c=9385
=139,b=9660, c=9661
=141,b=9940, c=9941
=143,b=10224, c=10225
=145,b=10512, c=10513
=147,b=10804, c=10805
=149,b=11100, c=11101
=151,b=11400, c=11401
=153,b=11704, c=11705
=155,b=12012, c=12013
=157,b=12324, c=12325
=159,b=12640, c=12641
=161,b=12960, c=12961
=163,b=13284, c=13285
=165,b=13612, c=13613
=167,b=13944, c=13945
=169,b=14280, c=14281
=171,b=14620, c=14621
=173,b=14964, c=14965
=175,b=15312, c=15313
=177,b=15664, c=15665
=179,b=16020, c=16021
=181,b=16380, c=16381
=183,b=16744, c=16745
=185,b=17112, c=17113
=187,b=17484, c=17485
=189,b=17860, c=17861
=191,b=18240, c=18241
=193,b=18624, c=18625
=195,b=19012, c=19013
=197,b=19404, c=19405
=199,b=19800, c=19801
=201,b=20200, c=20201由于程序中加入了是否为勾股数的判断,因此这个清单应该是准确的。这个小题目求解下来,我自己的感觉是做事的方法中算法还是至关重要的!
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》,《Python数据结构与算法教程》、《Python函数使用技巧总结》,《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
Python实现获取前100组勾股数的方法示例