ITPub博客

首页 > IT职业 > IT职场 > python中怎么对一个数进行因式分解?

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

IT职场 作者:cookye 时间:2021-09-11 10:16:12 0 删除 编辑


1、Python因式分解代码:

import time
# 对一个数进行因式分解
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("用时:", et - st)

2、因式分解思路:

假定要分解的整数为m

1、首先用while循环判断m是否大于1;

2、如果m>1再用for循环找到m的最小因数n,

用append()把最小因数添加到factor数组中;

3、把m/n赋给m,继续执行第二步;

4、直到m不大于1,返回数组factor。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1369/viewspace-2833540/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: pycharm下怎么用pip
请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    91
  • 访问量
    501