质因数分解
代码: #!/usr/bin/env python # unique factorization # e.g # 7 is a prime number # 6000 = [2^4]*[3^1]*[5*3] import sys def uniq_fac(num): ''' unique factorization of integer num return a tuple (flag, prime-list, index-list) flag: num is prime or not prime-list: [p1, p2, ...pr] index-list: [e1, e2, ...er] ''' if not isinstance(num, int): print num, 'not an integer' sys.exit(1) flag = 1 prime_list = [] index_list = [] for i in xrange(2, num/2+1): if num%i == 0: prime_list.append(i) idx = 0 while num%i == 0: num = num/i idx += 1 index_list.append(idx) flag = 0 return (flag, prime_list, index_list)