1、Python因式分解代碼:
importtime
#對一個數進行因式分解
deffactorization(num):
factor=[]
whilenum>1:
foriinrange(num-1):
k=i+2
ifnum%k==0:
factor.append(k)
num=int(num/k)
break
returnfactor
st=time.perf_counter()
print(factorization(707829217))
et=time.perf_counter()
print("用時:",et-st)
2、因式分解思路:
假定要分解的整數為m
1、首先用while循環判斷m是否大于1;
2、如果m>1再用for循環找到m的最小因數n,
用append()把最小因數添加到factor數組中;
3、把m/n賦給m,繼續執行第二步;
4、直到m不大于1,返回數組factor。
以上內容為大家介紹了python中怎么對一個數進行因式分解?希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。