新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:python中怎么對(duì)一個(gè)數(shù)進(jìn)行因式分解?
1、python因式分解代碼:

import time
# 對(duì)一個(gè)數(shù)進(jìn)行因式分解
def factorization(num):
factor = []
while num > 1:
for i in range(num - 1):
k = i + 2
if num % k == 0:
factor.append(k)
num = int(num / k)
break
return factor
st = time.perf_counter()
print(factorization(707829217))
et = time.perf_counter()
print("用時(shí):", et - st)
2、因式分解思路:
假定要分解的整數(shù)為m
1、首先用while循環(huán)判斷m是否大于1;
2、如果m>1再用for循環(huán)找到m的最小因數(shù)n,
用append()把最小因數(shù)添加到factor數(shù)組中;
3、把m/n賦給m,繼續(xù)執(zhí)行第二步;
4、直到m不大于1,返回?cái)?shù)組factor。
網(wǎng)頁(yè)題目:創(chuàng)新互聯(lián)Python教程:python中怎么對(duì)一個(gè)數(shù)進(jìn)行因式分解?
文章起源:http://m.5511xx.com/article/djejijp.html


咨詢(xún)
建站咨詢(xún)
