Python实现将一个正整数分解质因数的方法分析

作者:rosboy 时间:2021-01-09 10:39:29 

本文实例讲述了Python实现将一个正整数分解质因数的方法。分享给大家供大家参考,具体如下:

遇到一个python编程联系题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

版本一:

开始,没动脑子就开始写了,结果如下代码


#! /usr/bin/python
# 014.py
import math
number = int(raw_input("Enter a number: "))
while number != 1:
 for i in range(1, number + 1):
   if (number % i) == 0 and i != 1:
     number = number / i
     if number == 1:
       print " %d" %i
     else:
       print " %d*" %i,
     break

结果,输入9876543210这个十位数的时候,报错:

Traceback (most recent call last):
  File "./014.py", line 8, in <module>
    for i in range(1, number + 1):
OverflowError: range() result has too many items

版本二:

版本一报错是因为range有了太多的项,于是想着减少range出的list的项。由于,在判断一个数n是否是质数的时候,只需从2到n的平方根就行了,所以有了版本二,代码如下:


#! /usr/bin/python
# 014_1.py
import math
number = int(raw_input("Enter a number: "))
list = []
def getChildren(num):
 print '*'*30
 isZhishu = True
 for i in range(2, int(math.sqrt(1 + num)) + 1): #多加个1
   if num % i == 0 and i != num :
     list.append(i)
     isZhishu = False
     getChildren(num / i)
     break
 if isZhishu:
   list.append(num)
getChildren(number)
print list

这样,数字可以增大很多而不至于报错。但是 ,也是很有限度的,当输入大数如 123124324324134334 时,会导致内存不足,杀死进程

Traceback (most recent call last):
  File "./014_1.py", line 20, in <module                                            >
    getChildren(number)
  File "./014_1.py", line 11, in getChildren
    for i in range(2, int(math.sqrt(1 +  num)) + 1):
MemoryError

为了追求能对更大的数进行操作,猜想原因可能是递归调用时每次都需要建立一个很大的由range()建立的list,于是想避免range的使用,于是有了版本三:

版本三:

代码


#! /usr/bin/python
# 014_1.py
import math
number = int(raw_input("Enter a number: "))
list = []
def getChildren(num):
 print '*'*30
 isZhishu = True
 i = 2
 square = int(math.sqrt(num)) + 1
 while i <= square:
   if num % i == 0:
     list.append(i)
     isZhishu = False
     getChildren(num / i)
     i += 1
     break
   i += 1
 if isZhishu:
   list.append(num)
getChildren(number)
print list

同样对123124324324134334 进行操作,速度很快,得到如下结果

 Enter a number: 123124324324134334
******************************
******************************
******************************
******************************
******************************
[2, 293, 313, 362107, 1853809L]

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程序设计有所帮助。

来源:http://blog.csdn.net/rosboy/article/details/38542649

标签:Python,正整数,质因数
0
投稿

猜你喜欢

  • Python+Flask编写一个简单的行人检测API

    2023-09-26 17:55:19
  • python中open函数的基本用法示例

    2021-08-03 20:42:31
  • Python爬虫爬取博客实现可视化过程解析

    2023-12-16 08:58:33
  • Python利用随机函数生成变化图形详解

    2021-07-02 06:54:54
  • 长文章自动分页asp实例-支持HTML

    2007-10-10 21:29:00
  • 利用phpexcel对数据库数据的导入excel(excel筛选)、导出excel

    2023-09-04 13:50:42
  • Python 按字典dict的键排序,并取出相应的键值放于list中的实例

    2022-01-26 16:48:46
  • 为FCKeditor2.6添加行距功能(最新修改)

    2008-08-18 21:09:00
  • 中秋送礼分配不均这款python刮刮卡完美解决问题

    2023-01-03 11:28:38
  • 使用Dreamweaver MX表格排序功能

    2010-07-13 12:08:00
  • 教女朋友学Python3(二)简单的输入输出及内置函数查看 <font color=red>原创</font>

    2022-11-14 08:32:18
  • python实现Decorator模式实例代码

    2022-05-10 06:04:56
  • 使用Python处理Excel表格的简单方法

    2023-12-07 08:05:04
  • Python可视化工具如何实现动态图表

    2023-05-22 19:21:27
  • python实现蒙特卡罗方法教程

    2023-01-29 16:36:02
  • 解决Django中修改js css文件但浏览器无法及时与之改变的问题

    2023-10-20 05:54:08
  • ASP用户登录验证代码

    2008-05-15 12:49:00
  • 关于Python解包知识点总结

    2021-01-03 22:34:15
  • Linux-ubuntu16.04 Python3.5配置OpenCV3.2的方法

    2022-06-24 09:32:21
  • asp如何在页面中实现对电子信箱的访问?

    2010-06-26 12:34:00
  • asp之家 网络编程 m.aspxhome.com