题解 | #质数因子#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
递归可用迭代代替:
某个数的质子如果不是这个数本身,那么质子一定小于该数的开方
如果A%b!=0 ,A%c==0 且A//c=d 那么b一定不是d的质子
结合以上两点:
num=int(input()) index=2 high=math.sqrt(num) cache=[] while index<=high: if num%index==0: num=num//index#对num进行迭代,以此消除递归 high = math.sqrt(num)#遍历的上届应该为num的开方 cache.append(index) print(index,end=" ") else: index+=1#如果index不是num的质子,那也不可能时迭代后num的质子 print(num)