Python实现求最大公约数及判断素数的方法
作者:buaa_shang 时间:2021-06-13 02:20:20
本文实例讲述了Python实现求最大公约数及判断素数的方法。分享给大家供大家参考。具体实现方法如下:
#!/usr/bin/env python
def showMaxFactor(num):
count = num / 2
while count > 1:
if num % count == 0:
print 'largest factor of %d is %d' % (num, count)
break #break跳出时会跳出下面的else语句
count -= 1
else:
print num, "is prime"
for eachNum in range(10,21):
showMaxFactor(eachNum)
运行结果如下:
largest factor of 10 is 5
11 is prime
largest factor of 12 is 6
13 is prime
largest factor of 14 is 7
largest factor of 15 is 5
largest factor of 16 is 8
17 is prime
largest factor of 18 is 9
19 is prime
largest factor of 20 is 10
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,最大公约数,素数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python简单获取本机计算机名和IP地址的方法
2021-04-18 01:46:16
Python技法之如何用re模块实现简易tokenizer
2021-08-04 17:03:52
Python实现非正太分布的异常值检测方式
2021-11-22 12:27:57
![](https://img.aspxhome.com/file/2023/4/76054_0s.jpg)
Python字节码与程序执行过程详解
2022-01-25 04:45:24
![](https://img.aspxhome.com/file/2023/0/64600_0s.png)
MySQL安全性指南 (2)
2010-07-26 13:26:00
Javascript调用XML制作连动下拉框
2007-09-17 12:55:00
html元素input使用方法
2007-12-06 13:02:00
ORACLE数据库事务隔离级别介绍
2012-10-07 10:43:36
python数据可视化Seaborn画热力图
2022-01-17 22:55:05
![](https://img.aspxhome.com/file/2023/3/65063_0s.png)
ASP用户登录模块的设计源码
2008-10-03 12:16:00
Microsoft SQL Server数据库开发问题详解
2009-10-23 14:16:00
错误 2812: 未能找到存储过程 'master.dbo.xp_fileexist'
2010-07-22 19:50:00
python中实现指定时间调用函数示例代码
2021-04-20 07:14:25
Python实现小黑屋游戏的完整实例
2021-03-08 13:27:43
![](https://img.aspxhome.com/file/2023/5/67745_0s.png)
go语言优雅地处理error工具及技巧详解
2023-07-16 18:56:56
网站中美好的细节
2011-07-13 18:43:07
扫盲大讲堂:mysql出错的代码解析及解答
2009-09-05 10:08:00
在Heroku云平台上部署Python的Django框架的教程
2022-02-28 08:46:49
JavaScript中的64位加密及解密
2009-12-23 19:10:00
Pycharm学习教程(1) 定制外观
2021-08-01 05:18:11
![](https://img.aspxhome.com/file/2023/4/77584_0s.jpg)