Python实现求最大公约数及判断素数的方法

yipeiwu_com5年前Python基础

本文实例讲述了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日志模块logging基本用法分析

本文实例讲述了Python日志模块logging基本用法。分享给大家供大家参考,具体如下: 1. 基础用法 python提供了一个标准的日志接口,就是logging模块。日志级别有DEB...

Python3模拟curl发送post请求操作示例

本文实例讲述了Python3模拟curl发送post请求操作。分享给大家供大家参考,具体如下: 后端给的接口样式: curl "http://65.33.44.43:509/pre/u...

Python3之不使用第三方变量,实现交换两个变量的值

method 1: a,b = b,a method 2: a = a+b b = a-b a = a-b 以上这篇Python3之不使用第三方变量,实现交换两个变量的...

python获取标准北京时间的方法

本文实例讲述了python获取标准北京时间的方法。分享给大家供大家参考。具体分析如下: 这段python代码主要通过www.beijing-time.org的官网上获取标准的北京时间,如...

Matplotlib绘制雷达图和三维图的示例代码

Matplotlib绘制雷达图和三维图的示例代码

1.雷达图 程序示例 '''1.空白极坐标图''' import matplotlib.pyplot as plt plt.polar() plt.show()...