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

Python 練習實例14

Python 100例 Python 100例

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

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

程序源代碼:

實例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- def reduceNum(n): print '{} = '.format(n), if not isinstance(n, int) or n <= 0 : print '請輸入一個正確的數(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 一定是素數(shù) print '{} *'.format(index), break reduceNum(90) reduceNum(100)

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

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

Python 100例 Python 100例

其他擴展