• 技术文章 >常见问题 >Python常见问题

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

     Ly Ly2020-05-23 09:17:43原创9789

    1、Python因式分解代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    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。

    专题推荐:python
    上一篇:python3下怎么查看是否有pyqt5? 下一篇:python执行成功显示什么?

    相关文章推荐

    • python怎么判断是不是中文字符?• python函数里面形参和实参一样吗?• python安装后cmd找不到怎么办?• python安装以后如何运行?• python3怎么写自动关机?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网