• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python实现正整数分解质因数操作示例

python 搞代码 4年前 (2022-01-07) 19次浏览 已收录 0个评论

这篇文章主要介绍了Python实现正整数分解质因数操作,结合实例形式分析了Python循环与递归两种操作方法实现分解质因数功能的相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python实现正整数分解质因数操作。分享给大家供大家参考,具体如下:

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

 #!/usr/bin/env python # -*- coding: utf-8 -*- def div_func(n): result = [] while True: for i in xrange(2, int(n**0.5) + 1): if n % i == 0: result.append(i) n /= i break else: result.append(n) break return ' * '.join(map(str, result)) num = raw_input('please enter a number(  10**18 or int_num <0: raise valueerror() print div_func(int_num) except valueerror: 'invalid number' <pre></div><blockquote>please enter a number( <1.0E+18):123124324324134334<br /> 2 X 293 X 313 X 3621<strong style="color:transparent">来源gaodai#ma#com搞@代~码网</strong>07 X 1853809</p></blockquote><p>自己写的,完全没有参考网上其它人的算法。结果和大家都差不多。</p><p><strong>另外还可以用递归方法:</strong></p><div class="gaodaimacode"><pre class="prettyprint linenums"> def factor(num): if num == 1: return [] else: for i in range(2, num+1): n, d = divmod(num, i) if d == 0: return [i] + factor(n) for test_num in (299, 1024, 20, 7): print(test_num, '->', ' * '.join(map(str, factor(test_num)))) 

299 -> 13 * 23
1024 -> 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
20 -> 2 * 2 * 5
7 -> 7

PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:

在线分解质因数计算器工具:
http://tools.gaodaima.com/jisuanqi/factor_calc

在线一元函数(方程)求解计算工具:
http://tools.gaodaima.com/jisuanqi/equ_jisuanqi

科学计算器在线使用_高级计算器在线计算:
http://tools.gaodaima.com/jisuanqi/jsqkexue

在线计算器_标准计算器:
http://tools.gaodaima.com/jisuanqi/jsq

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

以上就是Python实现正整数分解质因数操作示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python实现正整数分解质因数操作示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址