我是靠谱客的博主 幸福导师,这篇文章主要介绍Python实现求最大公约数及判断素数的方法,现在分享给大家,希望可以做个参考。

本文实例讲述了Python实现求最大公约数及判断素数的方法。分享给大家供大家参考。具体实现方法如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
#!/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)

运行结果如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
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实现求最大公约数及判断素数的方法的全部内容,更多相关Python实现求最大公约数及判断素数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(139)

评论列表共有 0 条评论

立即
投稿
返回
顶部