本题来自 Project Euler 第21题:https://projecteuler.net/problem=21

'''
Project Euler: Problem 21: Amicable numbers
Let d(n) be defined as the sum of proper divisors of n
(numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair
and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110;
therefore d(220) = 284.
The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
Answer: 31626
''' def d(n): #计算数字n所有真因数之和
res = 0
for i in range(1, n//2+1):
if n/i == float(n//i):
res += i
return(res) lst = [] #亲和数列表
for i in range(1, 10000):
a = d(i)
b = d(a)
if b == i and b != a:
lst.append(i) res = 0 #所有亲和数之和
for i in range(len(lst)):
res += lst[i]
print(res)

首先需要明确两个数学概念:

  • 真因数(proper divisor):除去数字本身的所有因数(不要求是素数)。比如:12的所有真因数是:1、2、3、4、6
  • 亲和数(amicable number):先求出数字n所有真因数之和a,然后再求出数字a所有真因数之和b。如果 a != b 且 n == b,则 a、b、n都是亲和数

概念弄清楚了,接下来就好办了。既是求10000以内所有亲和数之和,当然首先得找出所有亲和数(汇总为列表 lst)。而想找出亲和数,首先得定义出函数 d(n),用来计算数字n的所有真因数之和。这之后,只要用之前所述的两个条件将10000以内所有数字遍历,就能找出所有亲和数了。

思路虽然清晰,但实际计算起来,还是费了些时间。想必是有更优的算法可以计算真因数之和吧,但……算了,我这数学渣,能算出来就不错了,不指望有啥好算法了……

Python练习题 048:Project Euler 021:10000以内所有亲和数之和的更多相关文章

  1. Python练习题 028:求3*3矩阵对角线数字之和

    [Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...

  2. Python练习题 038:Project Euler 010:两百万以内所有素数之和

    本题来自 Project Euler 第10题:https://projecteuler.net/problem=10 # Project Euler: Problem 10: Summation o ...

  3. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  4. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  5. Python练习题 046:Project Euler 019:每月1日是星期天

    本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...

  6. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  7. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  8. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  9. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

随机推荐

  1. 修改注册表修改环境变量PATH

      因公司办公电脑做了权限设置,不能通过右击“我的电脑”-属性的方式更改环境变量值 摸索找到了通过修改注册表的方式修改PATH值 Win+R 输入regedit打开册表编辑器 HKEY_LOCAL_M ...

  2. VS2015+opencv3.1.0 imshow()函数出现中文乱码----问题一

    Visual Studio提供高级保存选项功能,它能指定特定代码文件的编码规范和行尾所使用的换行符.在Visual Studio 2015中,该命令没有默认显示在“文件”菜单中.用户需要手工设置,才能 ...

  3. 万字长文,Python数据分析实战,使用Pandas进行数据分析

    文章目录 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大家 ...

  4. Pytest的装饰器——parametrize中ids里包含中文,用例标题显示异常如何解决?

    在使用pytest做测试的过程中,经常会用到pytest.mark.parametrize来对批量生成测试用例,比如 @pytest.mark.parametrize( ['a', 'b', 'exp ...

  5. 【转】Tomcat搭建文件服务器

    http://blog.csdn.net/yin_jw/article/details/43524659 1. 配置在tomcat目录下 直接把文件放在 tomcat6/webapps/ROOT 目录 ...

  6. 写shader小细节——这个会不断更新

    这个是因为自己被自己蠢哭了动笔的,里面大概记录自己所犯的错,和一些小知识. 1.有一个错误我经常犯:内部定义的字段没对应开放到编辑器的字段.这个是由于我有点依赖ide写代码的习惯导致,而shader的 ...

  7. unity3d android动态更新dll

    基本是参考这篇文章:http://blog.sina.com.cn/s/blog_9e5d42ee0102vvtg.html,进行了增删一波. 大略说一下基本步骤:1.下载mono源码,修改源码,编译 ...

  8. Weights and Measures (贪心+dp)

    I know, up on top you are seeing great sights, But down at the bottom, we, too, should have rights. ...

  9. 吐槽express 中间件multer

    工作不是那么忙,想学一下Express+multer弄一个最简单的文件上传,然后开始npm install,然后开始对着multer官方文档一顿操作. 前台页面最简单的: <!DOCTYPE h ...

  10. Nginx在mvvm模式中的使用