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

yipeiwu_com5年前Python基础

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

python程序 线程队列queue使用方法解析

一、线程队列 queue队列:使用方法同进程的Queue一样 如果必须在多个线程之间安全地交换信息时,队列在线程编程中尤其有用。 重要: q.put() :往队列里面放值,当参数blo...

python 中文件输入输出及os模块对文件系统的操作方法

整理了一下python 中文件的输入输出及主要介绍一些os模块中对文件系统的操作。 文件输入输出 1、内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文...

Python学习笔记之For循环用法详解

本文实例讲述了Python学习笔记之For循环用法。分享给大家供大家参考,具体如下: Python 中的For循环 Python 有两种类型的循环:for 循环和 while 循环。fo...

关于python列表增加元素的三种操作方法

 1、insert方法,该方法包含两个参数,第一个参数为插入的位置参数,第二个参数为插入内容 a = [0,0,0] b = [1,2,3] a.insert(0,b) p...

Python全栈之列表数据类型详解

前言 列表(list)同字符串一样都是有序的,因为他们都可以通过切片和索引进行数据访问,且列表是可变的。 创建列表的几种方法 第一种 name_list = ['Python',...