中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

Python 練習(xí)實(shí)例14

Python 100例 Python 100例

題目:將一個(gè)正整數(shù)分解質(zhì)因數(shù)。例如:輸入90,打印出90=2*3*3*5。

程序分析:對(duì)n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個(gè)最小的質(zhì)數(shù)k,然后按下述步驟完成:
(1)如果這個(gè)質(zhì)數(shù)恰等于n,則說(shuō)明分解質(zhì)因數(shù)的過(guò)程已經(jīng)結(jié)束,打印出即可。
(2)如果n<>k,但n能被k整除,則應(yīng)打印出k的值,并用n除以k的商,作為新的正整數(shù)你n,重復(fù)執(zhí)行第一步。
(3)如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步。

程序源代碼:

實(shí)例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- def reduceNum(n): print '{} = '.format(n), if not isinstance(n, int) or n <= 0 : print '請(qǐng)輸入一個(gè)正確的數(shù)字 !' exit(0) elif n in [1] : print '{}'.format(n) while n not in [1] : # 循環(huán)保證遞歸 for index in xrange(2, n + 1) : if n % index == 0: n /= index # n 等于 n/index if n == 1: print index else : # index 一定是素?cái)?shù) print '{} *'.format(index), break reduceNum(90) reduceNum(100)

以上實(shí)例輸出結(jié)果為:

90 =  2 * 3 * 3 * 5
100 =  2 * 2 * 5 * 5

Python 100例 Python 100例

其他擴(kuò)展