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. IE浏览器 多版本之间切换

    由于Win7系统最低支持IE8的版本,不能通过安装IE7版本来达到要求. 有一个替代方案,通过IE自带Emulation来实现,实现步骤如下: 打开现有的IE浏览器 按下F12键 切换到“Emulat ...

  2. ubuntu16.04下配置静态ip

    ubuntu下配置静态ip 1.先获取ip基本信息 ifconfig enp3s0 Link encap:以太网 硬件地址 2c:4d:54:65:de:6e inet 地址:192.168.199. ...

  3. springmvc的jar包

     <!-- spring框架的的组件构成(springFramework)--> 一.核心部分Core Container 1.1 spring-core,spring-beans 提供控 ...

  4. Coursera Algorithms week4 基础标签表 练习测验:Java autoboxing and equals

    1. Java autoboxing and equals(). Consider two double values a and b and their corresponding Double v ...

  5. hibernate基础学习---hierbnate2级缓存

    1:开启二级缓存sessionFactory需要安装jar包 2:在实体类配置文件添加(配置二级缓存).我的配置文件是Account.hbm.xml <?xml version="1. ...

  6. LuaBridge

    不能直接公开基类的函数,必须单独公开基类,并声明继承关系  deriveClass<Player, BaseController>("Player") 直接公开基类的函 ...

  7. Activity启动模式(GIF 动态演示)

    本文首发在我的个人微信公众号:Android开发圈 引言 关于Activity的启动模式是面试高频问题,在平时开发中,作用也不小,所以还是很有必要搞懂这一块的知识.其实之前也有写过这个主题的文章,但是 ...

  8. 记录第一次在egret项目中使用Puremvc

    这几天跟着另一个前端在做一个小游戏,使用的是egret引擎和puremvc框架,这对于我来说还是个比较大的突破吧,特此记录下. 因为在此项目中真是的用到了mvc及面向对象编程,值得学习 记录第一次在e ...

  9. Nginx 配置https请求

    通过阿里云生成指定的https证书文件xxxx.key 和 xxxx.pem文件 在阿里云上申请的https证书的是pem格式,转成cer 先在终端cd到文件目录下 然后 openssl x509 - ...

  10. ACM_三元一次函数解法(克莱姆法则)

    三元一次函数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 计算三元一次函数..如图 Input: EOF,先输入n,表示样例个 ...