首页 / 知识

python中怎么对一个数进行因式分解?

2023-11-12 13:27:00

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相关知识,请关注我们

培训代码因式分解个数因数

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢