异步,永不止步

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

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)

 

posted on 2016-03-03 10:07  gaopq  阅读(9493)  评论(0编辑  收藏  举报

导航