# 加载中...

• 博客等级：
• 博客积分：0
• 博客访问：56,206
• 关注人气：5
• 获赠金笔：0支
• 赠出金笔：0支
• 荣誉徽章：

## 完美数和亲和数Python和C

(2020-08-17 22:16:06)

### 亲和数和完美数

#include
#include

int getYSList(int number){
int returnValue = 1;
int i=2;
while(i
if(number % i == 0){
returnValue += i;
}
i++;
}
return returnValue;
}

int main(){
int number, sumOfys, i, sumOfsum, j;
clock_t start, finish;
scanf("%d", &number);
start=clock();
j = 0;
for(i=2;i
sumOfys = getYSList(i);
if(sumOfys>i){
sumOfsum = getYSList(sumOfys);
if(sumOfsum == i){
j++;
printf("Find the [ - ]对 亲和数: m 和 m\n", j, i, sumOfys);
}
}
}
finish=clock();
printf("亲和数计算时间: \t %f\n", (double)(finish-start)/CLOCKS_PER_SEC);
start=clock();
j = 0;
for(i=2;i
if(getYSList(i) == i){
j++;
printf("Find the [ %d ]个完美数: %d\n", j, i);
}
}
finish=clock();
printf("完美数计算时间: \t %f\n", (double)(finish-start)/CLOCKS_PER_SEC);
return 0;
}

#!/bin/env python3
## -*-coding: utf-8 -*-
from colorama import Fore, init
from time import time
from multiprocessing import Pool
import multiprocessing

# 设置查找亲和数的范围
MAX_NUMBER = 2000

# 得到因数列表
# 输入：number 需要计算其因数的数字
# 输出：retList 不包括其自身的因数列表
def getYSList(number):
retList = [1]
i = 2
while i < number:
if number % i == 0:
retList.append(i)
i += 1
# print(retList)
return retList

# 计算因数之和
def calSumOfYS(number):
returnValue = 1
i = 2
while i        if number % i == 0:
returnValue += i
i += 1
return returnValue

# 求亲和数方法ＩＩ
def calQinhe2(number_start, number_scope):
j = 0
for number in range(number_start, number_scope):
sumNumberYS = calSumOfYS(number)
if sumNumberYS <= number:
continue
if number == calSumOfYS(sumNumberYS):
j += 1
print("找到第",
Fore.BLUE + "[-]" % j,
"对亲和数:",
Fore.RED + "m" % number ,
" 和 ",
Fore.RED + "m" % sumNumberYS)

# 计算亲和数多线程
def calQinheM(number):
sumNumberYS = calSumOfYS(number)
if sumNumberYS > number:
if number == calSumOfYS(sumNumberYS):
print("找到亲和数:",
Fore.RED + "m" % number ,
" 和 ",
Fore.RED + "m" % sumNumberYS)

#计算完美数ＩＩ
def calWanmei2(number_start, number_scope):
j = 0
for number in range(number_start, number_scope):
if calSumOfYS(number) == number:
j += 1
print("找到第",
Fore.CYAN + "[-]" % j,
"个完美数:",
Fore.RED + "m" % number)

# 计算完美数多线程
def calWanmeiM(number):
if calSumOfYS(number) == number:
print("找到完美数:",Fore.RED + "m" % number)

if __name__ == '__main__':
# 找亲和数
init(autoreset=True)

number_scope = int(input("请输入计算范围: " ) or MAX_NUMBER)
pool = Pool(multiprocessing.cpu_count())

print(Fore.BLUE + ("#"*10 + "找亲和数" + "#"*10))

beginT = time()
calQinhe2(2, number_scope)
endT = time()
print("calQinhe2 用了 %.5f 秒" % (endT-beginT))

beginT = time()
pool.map(calQinheM, [i for i in range(2, number_scope)])
endT = time()
print("calQinheM 用了 %.5f 秒" % (endT-beginT))

# 找完美数
print("\n\n\n")

beginT = time()
calWanmei2(2, number_scope)
endT = time()
print("calWanmei2 用了 %.5f 秒" % (endT-beginT))

beginT = time()
params = [ i for i in range(2, number_scope)]
#calWanmei2(2, number_scope)
pool.map(calWanmeiM, params)
endT = time()
print("calWanmeiM 用了 %.5f 秒" % (endT-beginT))

0

• 评论加载中，请稍候...

发评论

以上网友发言只代表其个人观点，不代表新浪网的观点或立场。

新浪BLOG意见反馈留言板　电话：4000520066 提示音后按1键（按当地市话标准计费）　欢迎批评指正

新浪公司 版权所有