编程测试题:
输入一个正整数将其分解成素数的乘积,输入格式连续输入m个数,然后将这m个数分别分解,如
输入:
2 10 20
输出:
2 5 2 2 5
Python code:
def primes(n): primfac = [] d = 2 while d*d <= n: while (n % d) == 0: primfac.append(d) n //= d d += 1 if n > 1: primfac.append(n) return primfac s = int(raw_input()) i=0 fac=[] N = [] while i<s: N = N + [int(raw_input())] i = i+1 i=0 while i<s: j=0 fac = fac+[primes(N[i])] for j in range(len(fac[i])): print fac[i][j], print i=i+1