Python实现的对一个数进行因式分解操作示例
作者:一应常怀 时间:2023-01-11 04:30:40
本文实例讲述了Python实现的对一个数进行因式分解操作。分享给大家供大家参考,具体如下:
在数学中,我们可能会对一个数进行因式分解,如何用Python来实现呢?以下是某位大佬写的算法,这里拿过来直接用就可以了。
# 对一个数进行因式分解
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
我们调用这个函数,并引入time库进行时间计算
st = time.perf_counter()
print(factorization(707829217))
et = time.perf_counter()
print("用时:", et - st)
可以看到最后的打印结果为:
[8171, 86627]
用时: 0.0064456
可以看到因式分解还是非常快的,所以在这里非常感谢这位大佬啦~
最后附上完整版,供大家使用(复制到你的IDE中,然后只要修改数字即可)
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)
运行结果:
[8171, 86627]
用时: 0.039954294630645655
PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:
在线分解质因数计算器工具:
http://tools.jb51.net/jisuanqi/factor_calc
在线一元函数(方程)求解计算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科学计算器在线使用_高级计算器在线计算:
http://tools.jb51.net/jisuanqi/jsqkexue
在线计算器_标准计算器:
http://tools.jb51.net/jisuanqi/jsq
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/qq_39321542/article/details/89287248
标签:Python,因式分解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python编写softmax函数、交叉熵函数实例
2023-11-24 07:08:11
![](https://img.aspxhome.com/file/2023/6/64606_0s.jpg)
PHP实时统计中文字数和区别
2023-07-13 10:44:01
python 读写文件包含多种编码格式的解决方式
2022-01-12 18:02:10
利用Python-iGraph如何绘制贴吧/微博的好友关系图详解
2022-02-26 07:16:32
![](https://img.aspxhome.com/file/2023/6/109456_0s.png)
利用global.asa计划执行程序
2008-03-05 12:49:00
python之tensorflow手把手实例讲解斑马线识别实现
2021-11-11 05:53:19
![](https://img.aspxhome.com/file/2023/0/68460_0s.png)
利用python实现在微信群刷屏的方法
2023-05-01 13:56:29
![](https://img.aspxhome.com/file/2023/8/71498_0s.jpg)
javascript window.opener的用法分析
2024-05-05 09:22:49
python制作抖音代码舞
2021-09-05 21:51:00
![](https://img.aspxhome.com/file/2023/8/105088_0s.jpg)
Numpy中array数组对象的储存方式(n,1)和(n,)的区别
2022-08-28 03:08:32
![](https://img.aspxhome.com/file/2023/7/107517_0s.png)
使用python 进行区间取值的方法
2023-12-17 01:27:56
![](https://img.aspxhome.com/file/2023/4/70114_0s.png)
rollup3.x+vue2打包组件的实现
2024-05-22 10:43:40
![](https://img.aspxhome.com/file/2023/0/123210_0s.jpg)
pandas中按行或列的值对数据排序的实现
2023-09-23 02:32:18
java自定义动态链接数据库示例
2024-01-27 13:05:29
Python的gevent框架的入门教程
2023-06-26 16:58:34
Django调用百度AI接口实现人脸注册登录代码实例
2023-10-13 23:07:14
Sql语句优化注意
2009-10-31 13:15:00
python数组如何添加整行或整列
2022-04-29 16:39:30
js中的replace方法使用介绍
2024-04-10 13:54:58
Ubuntu下Python+Flask分分钟搭建自己的服务器教程
2023-11-12 04:37:43
![](https://img.aspxhome.com/file/2023/2/63182_0s.jpg)