Problem 21

https://projecteuler.net/problem=21

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.

如果a的因子之和等于b,b的因子之和等于a,并且a不等于b,那么a和b称为亲和数。

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.

计算10000以下的所有亲和数之和。

  1. import time
  2.  
  3. def is_amicable_number(num1, num2, divisors_num1, divisors_num2):
  4. if sum(divisors_num1) != num2:
  5. return False
  6. if sum(divisors_num2) != num1:
  7. return False
  8. return True
  9.  
  10. def find_divisors(num):
  11. divisors = []
  12. for i in range(1, num//2+1):
  13. if num % i == 0:
  14. divisors.append(i)
  15. return divisors
  16.  
  17. start_time = time.ctime()
  18. amicable_numbers = []
  19. for x in range(1, 10001):
  20. x_divisors = find_divisors(x)
  21. for y in range(x//2, x):
  22. y_divisors = find_divisors(y)
  23. print('Searching for amicable numbers({x}?{y})...'.format(x=x, y=y))
  24. if is_amicable_number(x, y, x_divisors, y_divisors):
  25. print('Amicable numbers:', x, ':', y)
  26. amicable_numbers.append([x, y])
  27. end_time = time.ctime()
  28. print(start_time)
  29. print(end_time)
  30. print(amicable_numbers)
  31. tot = 0
  32. for i in amicable_numbers:
  33. tot += sum(i)
  34. print(tot)

结果:

10000以下的亲和数:[[284, 220], [1210, 1184], [2924, 2620], [5564, 5020], [6368, 6232]]
31626

Problem 21的更多相关文章

  1. (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 ...

  2. [LeetCode&Python] Problem 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. uoj problem 21 缩进优化

    题目: 小O是一个热爱短代码的选手.在缩代码方面,他是一位身经百战的老手.世界各地的OJ上,很多题的最短解答排行榜都有他的身影.这令他感到十分愉悦. 最近,他突然发现,很多时候自己的程序明明看起来比别 ...

  4. ●UOJ 21 缩进优化

    题链: http://uoj.ac/problem/21 题解: ...技巧题吧 先看看题目让求什么: 令$F(x)=\sum_{i=1}^{n}(\lfloor a[i]/x \rfloor +a[ ...

  5. UOJ#21 【UR #1】缩进优化

    传送门 http://uoj.ac/problem/21 枚举 (调和级数?) $\sum_{i=1}^{n} (a_i / x + a_i \bmod x) =\sum a_i - (\sum_{i ...

  6. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  7. electrica writeup

    关于 caesum.com 网上上的题目,分类有Sokoban,Ciphers,Maths,Executables,Programming,Steganography,Misc.题目有点难度,在努力奋 ...

  8. The 2016 ACMICPC Asia Beijing Regional Contest

    A. Harmonic Matrix Counter (3/19) B. Binary Tree (1/14) C. Asa's Chess Problem (21/65) [ Problem ] 给 ...

  9. [xjtu21]wmq的午餐 计数问题

    http://oj.xjtuacm.com/problem/21/ 对13进行分析,每种价格出现的次数: $(C_m^1 + C_m^2 + ... + C_m^m)(C_{n - m}^0 + C_ ...

随机推荐

  1. vue cli webstorm

    贫富差距的产生 – 目光博客 http://eyehere.net/2017/1947/ https://github.com/vuejs/vue Vue 2.0 Hello World - JSFi ...

  2. codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解

    秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...

  3. Faas 典型场景——应用负载有显著的波峰波谷,典型用例-基于事件的数据处理

    Serverless适用的两大场景 场景一:应用负载有显著的波峰波谷 Serverless化与否的评判标准并不是公司规模的大小,而是其业务背后的具体技术问题,比如业务波峰波谷明显,如何实现削峰填谷.一 ...

  4. 53. 部门信息显示 EXTJS 单击树节点

    1. /** * @author sux * @time 2011-1-14 * @desc 部门信息显示 */ deptInfoGridPanel = Ext.extend(Ext.grid.Edi ...

  5. Flink之Window Operation

    目录 Configuring Time Characteristics Process Functions Window Operators Applying Functions on Windows ...

  6. sql数据库CHECKDB时报x个分配错误和x个一致性错误

    --1.在SQL查询分析器中执行以下语句:(注以下所用的POS为数据库名称,请用户手工改为自己的数据库名) use pos dbcc checkdb --2.查看查询结果,有很多红色字体显示,最后结果 ...

  7. PCB 规则引擎之脚本语言JavaScript应用评测

    世界上没有好做的软件,觉得好做,只是你的系统简单而已,而不是哪个行业简单,特别像我们PCB制造企业务逻辑的很复杂的,仅仅靠决策树中的每个节点布置决策逻辑是不能满足要求的,所以我们在制作PCB规则引擎必 ...

  8. Potted Flower(线段树+dp)

    http://poj.org/problem?id=2750 题意:在一个圈中取若干个相邻的数,求他们的最大序列和.不能够同时取所有的数. 看了一篇解题报告写的很详细..http://blog.csd ...

  9. SpringMvc快速入门之使用篇

    文章是为了结合工作需求来介绍springmvc,本文章只是切合实际的开发的场景对springmvc进行快速的入门介绍. 本篇文章不会对原理进行讲解.因为个人觉得有些对于新技术方面可以分为一下几个层次. ...

  10. .net 必看书籍1

    我们2个网站运营群,有很多技术高手,同时也有大部分技术新人,如何从传统asp转到.net,从传统table转到div+css布局,从传统技术转到ajax,从小型程序转到高性能并发的大型程序,我花了2小 ...