Python实现的寻找前5个默尼森数算法示例
作者:快递小可 时间:2023-05-15 17:34:29
本文实例讲述了Python实现的寻找前5个默尼森数算法。分享给大家供大家参考,具体如下:
找前5个默尼森数。
若P是素数且M也是素数,并且满足等式M=2**P-1,则称M为默尼森数。例如,P=5,M=2**P-1=31,5和31都是素数,因此31是默尼森数。
python2代码如下:
from math import sqrt
def isPrime(n):
'judge whether a positive integer is a prime number!'
if n==1:
return False
k=int(sqrt(n))
for i in range(2,k+1):
if n%i==0:
return False
return True
def getMonisen(n):
count=0
l=[]
P=2#最小的素数是2,因此P初值为2
while True:
if isPrime(P):
M=2**P-1
if isPrime(M):
l.append(M)
count+=1
if count==n:
break
P+=1
return l
l=getMonisen(5)
print l
运行程序,结果如下:
[3, 7, 31, 127, 8191]
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/sxingming/article/details/51317602
标签:Python,默尼森数,算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python爬虫之urllib库详解
2022-01-09 23:03:27
![](https://img.aspxhome.com/file/2023/4/76174_0s.png)
Python 错误和异常小结
2021-08-19 12:17:58
Python随机生成带特殊字符的密码
2021-10-07 02:48:05
有关将idea的系统配置文件移到其它盘激活失效的问题
2022-02-16 09:33:37
Oracle 数据显示 横表转纵表
2009-07-26 08:57:00
快速解决vue.js 模板和jinja 模板冲突的问题
2023-04-04 12:49:59
![](https://img.aspxhome.com/file/2023/5/81785_0s.jpg)
Python 多线程共享变量的实现示例
2022-12-17 09:39:10
python 中关于pycharm选择运行环境的问题
2021-09-01 21:56:10
![](https://img.aspxhome.com/file/2023/4/75264_0s.jpg)
python Web开发你要理解的WSGI & uwsgi详解
2021-02-04 08:46:38
![](https://img.aspxhome.com/file/2023/1/72981_0s.png)
使用pandas计算环比和同比的方法实例
2021-02-15 18:06:34
![](https://img.aspxhome.com/file/2023/1/92331_0s.png)
python求绝对值的三种方法小结
2023-04-17 19:57:09
![](https://img.aspxhome.com/file/2023/0/72820_0s.jpg)
Python开发装包八种方法详解
2021-01-12 22:27:28
解决Navicat for MySQL 连接 MySQL 报2005错误的问题
2024-01-16 13:13:26
![](https://img.aspxhome.com/file/2023/1/115551_0s.png)
Microsoft Sql server2005的安装步骤图文详解及常见问题解决方案
2024-01-22 07:23:00
![](https://img.aspxhome.com/file/2023/5/113605_0s.png)
游戏开发Unity2D图片任意形状破碎裂片效果展示
2022-01-18 04:43:15
![](https://img.aspxhome.com/file/2023/1/132421_0s.gif)
MySQL数据库备份恢复的两个实用方法
2008-12-31 15:09:00
jetbrains mono字体安装方法(推荐)
2023-08-28 23:11:38
![](https://img.aspxhome.com/file/2023/0/131820_0s.jpg)
Python闭包装饰器使用方法汇总
2022-12-21 05:47:17
Python求两个文本文件以行为单位的交集、并集与差集的方法
2021-12-25 09:12:18
Git常用命令介绍
2022-08-14 03:42:46