current position:Home>Use python to live the whole life (6) - complete number

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

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

目录

一、前言

二、what is a perfect number

1、定义

2、规律

3、梅森素数

三、版本(1.0):硬算

四、版本1.1:end of number detection

五、版本1.2:除以9侦测

六、版本2.0:Mersenne prime detection

六、尾声


The content of this article is original,抄袭必究!!!!!!!!!!!

一、前言

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、定义

老规矩,Let's first understand what a perfect number is.

完全数,perfect number,定义为:all factors of this number(does not include the number itself)Add up exactly to this number.比如6就是完全数,因为6的因数有1,2,3(不包括6本身),1+2+3正好等于6.

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??

有,并且很多.(The following law is just that the perfect numbers found so far conform to this law,partially unproven)

第一,完全数都是以6或28结尾的.

第二,Perfect numbers are triangular numbers,例如6可以表示成1+2+3,28可以表示成1+2+3+4+5+6+7.

第三,除了6can be expressed as a continuous interval2sum of odd cubes.例如28表示成1^3+3^3,496表示成1^3+3^3+5^3+7^3.

第四,The sum of the reciprocals of all the factors of a perfect number is2.例如6The reciprocal of all factors is1,1/2,1/3,1/6,相加为2.28The reciprocal of all factors is1,1/2,1/4,1/7,1/14,1/28,相加为2.

第五,A perfect number can be expressed as2The sum of consecutive powers of.例如6可以表示成2^1+2^2,28可以表示成2^2+2^3+2^4.

第六,6After rolling the perfect numbers other than1.Rolling is to add up all the digits until only one digit remains.例如28Rolling number is:2+8=10,1+0=1.496Rolling number is:4+9+6=19,1+9=10,1+0=1.

第七,6A perfect number other than divided by9A certain remainder is1.例如:28除以9=3…1,496除以9=55…1.

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.

Let's think about it.

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

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):硬算

接下来,Let's write the program first.

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.

我们要创建一个函数,用for循环和rangeSet the number you are looking for,If the number is to look for digital factor,just use a variable to increment.After the detection is over, whether the detection factor is equal to the number to be looked for,返回真或假.

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~

补全代码,First ask where to detect,之后while循环或者for+range来计数,Call function to get information,十分的简单.

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

从上文我们可以知道,The end of the perfect number is6或者28,这样的话,We can save runtime again.

有人问了:诶诶诶,How do you know what the end of an integer is??

很简单,Turn it into a string and then intercept it..

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.

六、尾声

写作不易,Give the author a triple!

Guess why I broke the update yesterday?

Because I thought I had posted this article yesterday...就很无语,唉,这是天意,Let him break it(bushi

-----------------------------------------------------------end------------------------------------------------------------

copyright notice
author[¿¿¿¡¡¡],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2023/008/202301072259397563.html

Random recommended