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以下的所有亲和数之和。

import time

def is_amicable_number(num1, num2, divisors_num1, divisors_num2):
if sum(divisors_num1) != num2:
return False
if sum(divisors_num2) != num1:
return False
return True def find_divisors(num):
divisors = []
for i in range(1, num//2+1):
if num % i == 0:
divisors.append(i)
return divisors start_time = time.ctime()
amicable_numbers = []
for x in range(1, 10001):
x_divisors = find_divisors(x)
for y in range(x//2, x):
y_divisors = find_divisors(y)
print('Searching for amicable numbers({x}?{y})...'.format(x=x, y=y))
if is_amicable_number(x, y, x_divisors, y_divisors):
print('Amicable numbers:', x, ':', y)
amicable_numbers.append([x, y])
end_time = time.ctime()
print(start_time)
print(end_time)
print(amicable_numbers)
tot = 0
for i in amicable_numbers:
tot += sum(i)
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. Linux gadget驱动分析2------设备识别过程

    设备连上主机之后,设备驱动做了的事. 设备连上host的port之后,主机端会有一套策略发送请求获取device的一系列描述符.进行枚举过程.找到适合该device的驱动. 这样就可以与device进 ...

  2. Android 自带Base64加密解密

    Android项目引用不到以下两个java类 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; Android有自己的base ...

  3. 深入理解7816(3)-----关于T=0 【转】

    本文转载自:http://blog.sina.com.cn/s/blog_4df8400a0102vcyp.html 深入理解7816(3)-----关于T=0 卡片和终端之间的数据传输是通过命令响应 ...

  4. bzoj4889

    http://www.lydsy.com/JudgeOnline/problem.php?id=4889 人傻常数大 bzoj上跑不过 洛谷上能过两到三个点 我写的是树套树啊 怎么跑的比分块还慢 每次 ...

  5. openStack aio nova service-list neutron ext-list

  6. Vue.prototype的用法

    基础事例: 在vue项目main.js文件中: Vue.prototype.$appName = 'My App' 这样你可以通过在原型上定义它们使其在每个 Vue 的实例中可用. new Vue({ ...

  7. vs2010 下使用C#开发activeX控件

    1.创建一个类库 2.项目属性-应用程序-程序集信息-"使程序集COM可见"勾上; 3.项目属性-生成-"为COM互操作注册"勾上.(这个折腾一天,否则注册事件 ...

  8. PCB javascript解析钻孔(Excellon)格式实现方法

    解析钻孔(Excellon)格式前首先得了解此格式,这样才能更好的解析呀. 一个钻孔里面包含的基本信息如下: 1.单位:公式mm,英制inch 2.省零方式:前省零,后省零 3.坐标方式:绝对坐标,相 ...

  9. E20170705-hm

    bubble   n. 泡,水泡; 冒泡,起泡; 泡影,妄想; (欲表达的) 一点感情;

  10. Three Kingdoms(优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=3442 题意:ABCD有各自的攻击力与攻击范围,刘备只能走"C"与".", ...