【python】将一个正整数分解质因数

时间:2016-03-03 10:15:24   收藏:0   阅读:1974
def reduceNum(n):
    ‘‘‘题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5‘‘‘
    print {} = .format(n),
    if not isinstance(n, int) or n <= 0 :
        print Please input a valid number !
        exit(0)
    elif n in [1] :
        print {}.format(n)
    while n not in [1] : # 循环保证递归
        for index in xrange(2, n + 1) :
            if n % index == 0:
                n /= index # let n equal to it n/index
                if n == 1: # This is the point
                    print index # The last one
                else : # index 一定是素数
                    print {} *.format(index),
                break
reduceNum(1250895)
reduceNum(23)

 

原文:http://www.cnblogs.com/peiqianggao/p/5237470.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!