# Use python to live the whole life (6) - complete number

2023-01-08 00:59:05¿¿¿¡¡¡

1、定义

2、规律

3、梅森素数

# 一、前言

roll it up,new year's day has passed,Start writing articles.

Idle at home these days have nothing to do,just go to school every day,The only thing that's fun is three：学python、Play games and wait for Pleasant Goat's new drama（bushi.

This article will explain in detail the various detections of perfect numbers.写作不易,支持一波～

# 二、what is a perfect number

## 1、定义

So if someone buckles you6,That means he's praising you for being perfect.（bushi.

Perfect numbers were proposed by a man named Pythagoras,被誉为“The oldest mathematical problems”,This person also proposed the familiar Pythagorean theorem and the golden ratio.

Found a total of51个完全数,Very rare,Smaller ones have6、28、496、8128、33550336等等.

No one has found an odd perfect number yet,no one can prove“no odd perfect numbers”.但是,austin·Orr's people prove it：If there were the perfect number,must be expressible as12x质数+1或者36x质数+9,并且在10^300There is no odd perfect number in.

Perfect number will be more and more big,第39a perfect number has25674127位数,If printed in font size four,can also be turned into a dictionary.

## 2、规律

Are there any rules among these numbers?？

Do you know why there is an alias called perfect number?？太完美了！

## 3、梅森素数

Then came a Mersenne prime,This is made by Euclid.我们定义P是一个质数,如果2^p-1也是质数,Then the prime number is“梅森素数”.

After know mersenne prime,把P带入公式2^（p-1）（2^p-1）,Kaka is a,The result is a perfect number.

3是质数,2^3-1是7也是质数,那么7就是梅森素数.把3带入公式,After a click, the result is28.

Euler proves it,All perfect number conforming to this form.With this formula is easier.

# 三、版本（1.0）:硬算

We need to let the program find every factor of a number except itself,Put them all together,These procedures can be placed inside a function.Then put on the loop,Just repeat the number auto-increment call.是不是很简单？

The effect of completing all the factors of finding the number first.

``````def find(find_number):#新建函数findFind factors and judge
he=0#初始化变量
for i in range(1,find_number):#循环find_number次
if find_number%i==0:#如果i是find_number的因数
he=he+i#赋值
#这时候,he就是find_numbersum of all factors
if he==find_number:#比较
return True
else:
return False``````

The most critical part has been done,Complete the code, you can try it yourself～

This is the complete program：

``````def find(find_number):#新建函数findFind factors and judge
he=0#初始化变量
for i in range(1,find_number):#循环find_number次
if find_number%i==0:#如果i是find_number的因数
he=he+i
#这时候,he就是find_numbersum of all factors
if he==find_number:#比较
return True
else:
return False
a=int(input("输入要检测1a perfect number to how many digits"))
for i in range(1,a+1):
if find(i):
print(i,"是完全数")``````

# 四、版本1.1:end of number detection

``````def find(find_number):#新建函数findFind factors and judge
he=0#初始化变量
for i in range(1,find_number):#循环find_number次
if find_number%i==0:#如果i是find_number的因数
he=he+i
#这时候,he就是find_numbersum of all factors
if he==find_number:#比较
return True
else:
return False
a=int(input("输入要检测1a perfect number to how many digits"))
for i in range(1,a+1):
if str(i)[-1]=='6' or str(i)[-1]=='8':
if find(i):
print(i,"是完全数")``````

This will run twice as fast, right?.

# 五、版本1.2:除以9侦测

perfect number divided by9都余1,We can also use this to speed things up.不过,千万不要忽略“排除6”,one more6的侦测.looks more cumbersome,But doing so is nearly as fast as3倍速度.

``````def find(find_number):#新建函数findFind factors and judge
he=0#初始化变量
for i in range(1,find_number):#循环find_number次
if find_number%i==0:#如果i是find_number的因数
he=he+i
#这时候,he就是find_numbersum of all factors
if he==find_number:#比较
return True
else:
return False
a=int(input("输入要检测1a perfect number to how many digits"))
for i in range(1,a+1):
if str(i)[-1]=='6' or str(i)[-1]=='8':
if i%9==1 or i==6:
if find(i):
print(i,"是完全数")``````

# 六、版本2.0:Mersenne prime detection

This is the last ultimate method,is to find Mersenne primes.

First of all, it is necessary to detect the large cycle of prime numbers,ifTo determine whether a prime number is a Mersenne prime,The output is then plug in formula,十分的简单.

In the previous article on Goldbach's conjecture, there is already a prime number detection function,这里直接拿过来用,诶嘿.

c,运行太快了,Can't stop,诶！

（哔~）

Let's just add another wait time,有点快了.

``````from time import sleep
zhishu=[]#list of prime numbers
for i in range(2,10000):#Cycle through prime numbers
for j in range(2,i-1):#2到i内的每一个数
if i%j==0:#如果i不是质数
break#退出循环
else:#If the loop ends normally it isi是质数
zhishu.append(i)#zhishu添加i

for shu in zhishu:
if 2**shu-1 in zhishu:
print(2**(shu-1)*(2**shu-1),"是完全数")
sleep(1)``````

But this detection has a fatal flaw——只能检测10000以内的,因为我们用的是in来判断2**shu-1是不是质数,大于10000就没有了,If there is a prime number table data,I'm sure we can find a dozen of them.

So all eyes and other applications.

# 六、尾声

Guess why I broke the update yesterday？