Python实现获取前100组勾股数的方法示例


Posted in Python onMay 04, 2018

本文实例讲述了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 = 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 = 111, b = 6160, c = 6161
a = 113, b = 6384, c = 6385
a = 115, b = 6612, c = 6613
a = 117, b = 6844, c = 6845
a = 119, b = 7080, c = 7081
a = 121, b = 7320, c = 7321
a = 123, b = 7564, c = 7565
a = 125, b = 7812, c = 7813
a = 127, b = 8064, c = 8065
a = 129, b = 8320, c = 8321
a = 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 = 10225
a = 145, b = 10512, c = 10513
a = 147, b = 10804, c = 10805
a = 149, b = 11100, c = 11101
a = 151, b = 11400, c = 11401
a = 153, b = 11704, c = 11705
a = 155, b = 12012, c = 12013
a = 157, b = 12324, c = 12325
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 = 13944, c = 13945
a = 169, b = 14280, c = 14281
a = 171, b = 14620, c = 14621
a = 173, b = 14964, c = 14965
a = 175, b = 15312, c = 15313
a = 177, b = 15664, c = 15665
a = 179, b = 16020, c = 16021
a = 181, b = 16380, c = 16381
a = 183, b = 16744, c = 16745
a = 185, b = 17112, c = 17113
a = 187, b = 17484, c = 17485
a = 189, b = 17860, c = 17861
a = 191, b = 18240, c = 18241
a = 193, b = 18624, c = 18625
a = 195, b = 19012, c = 19013
a = 197, b = 19404, c = 19405
a = 199, b = 19800, c = 19801
a = 201, b = 20200, c = 20201

由于程序中加入了是否为勾股数的判断,因此这个清单应该是准确的。这个小题目求解下来,我自己的感觉是做事的方法中算法还是至关重要的!

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python中的自定义函数学习笔记
Sep 23 Python
python修改注册表终止360进程实例
Oct 13 Python
跟老齐学Python之模块的加载
Oct 24 Python
python按行读取文件,去掉每行的换行符\n的实例
Apr 19 Python
python在回调函数中获取返回值的方法
Feb 22 Python
Python实现获取系统临时目录及临时文件的方法示例
Jun 26 Python
Python+Selenium使用Page Object实现页面自动化测试
Jul 14 Python
python基于socket进行端口转发实现后门隐藏的示例
Jul 25 Python
Python的缺点和劣势分析
Nov 19 Python
详解PyQt5信号与槽的几种高级玩法
Mar 24 Python
浅析PyCharm 的初始设置(知道)
Oct 12 Python
Python django中如何使用restful框架
Jun 23 Python
python XlsxWriter模块创建aexcel表格的实例讲解
May 03 #Python
Python 中的Selenium异常处理实例代码
May 03 #Python
Python读写/追加excel文件Demo分享
May 03 #Python
windows下安装Python的XlsxWriter模块方法
May 03 #Python
python使用openpyxl库修改excel表格数据方法
May 03 #Python
python实现跨excel的工作表sheet之间的复制方法
May 03 #Python
Python利用openpyxl库遍历Sheet的实例
May 03 #Python
You might like
PHP如何得到当前页和上一页的地址?
2006/11/27 PHP
GBK的页面输出JSON格式的php函数
2010/02/16 PHP
php access 数据连接与读取保存编辑数据的实现代码
2010/05/12 PHP
php中用数组的方法设置cookies
2011/04/21 PHP
php基于str_pad实现卡号不足位数自动补0的方法
2014/11/12 PHP
yii2.0实现pathinfo的形式访问的配置方法
2016/04/06 PHP
PHP中实现中文字串截取无乱码的解决方法
2018/05/29 PHP
jQuery学习7 操作JavaScript对象和集合的函数
2010/02/07 Javascript
javascript同步服务器时间和同步倒计时小技巧
2015/09/24 Javascript
Windows下用PyCharm和Visual Studio开始Python编程
2015/10/26 Javascript
JavaScript脚本库编写的方法
2015/12/09 Javascript
jQuery获取单击节点对象的方法
2016/06/02 Javascript
学习vue.js表单控件绑定操作
2016/12/05 Javascript
Angular JS 生成动态二维码的方法
2017/02/23 Javascript
NodeJS安装图文教程
2018/04/19 NodeJs
jQuery实现的简单拖拽功能示例【测试可用】
2018/08/14 jQuery
js canvas实现二维码和图片合成的海报
2020/11/19 Javascript
详解vue 2.6 中 slot 的新用法
2019/07/09 Javascript
Python编程flask使用页面模版的方法
2018/12/28 Python
对Python的交互模式和直接运行.py文件的区别详解
2019/06/29 Python
Python如何实现转换URL详解
2019/07/02 Python
对Django项目中的ORM映射与模糊查询的使用详解
2019/07/18 Python
Python使用Beautiful Soup爬取豆瓣音乐排行榜过程解析
2019/08/15 Python
简单了解python中的与或非运算
2019/09/18 Python
Django 用户登陆访问限制实例 @login_required
2020/05/13 Python
CSS3制作半透明边框(Facebox)类似渐变
2012/12/09 HTML / CSS
CSS3 实现的加载动画
2020/12/07 HTML / CSS
莱德杯高尔夫欧洲官方商店:Ryder Cup Shop
2019/08/14 全球购物
银行柜员应聘推荐信范文
2013/11/24 职场文书
高校辅导员推荐信范文
2013/12/25 职场文书
医学求职信
2014/05/28 职场文书
亮剑观后感300字
2015/06/05 职场文书
投诉书格式范本
2015/07/02 职场文书
户外拓展训练感想
2015/08/07 职场文书
关于JS中的作用域中的问题思考分享
2022/04/06 Javascript
python三子棋游戏
2022/05/04 Python