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. iOS 中删除cookie的正确做法

    需求:删除 qq 登录的 cookie,保证下次打开 qq 登录页面不会默认登录 实现:      NSString *url =@"https://w.mail.qq.com/cgi-bi ...

  2. bzoj1690

    二分+分数规划+dfs判环 跟1486很像,但是我忘记怎么判环了, 我们可以写一个dfs,如果当前节点的距离小于更新的距离,而且这个点已经在当前访问过了,那么就是有环了,如果没有访问过就继续dfs,每 ...

  3. 69.资金管理-税率表管理extjs 页面

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...

  4. (function(){})();和(function(){}())每个括号的用途和区别

    (function(){…})(); 这种写法是因为JS中没有块级作用域的概念,所以可以用lambda函数来模仿块级作用域,这个的作用是定义并立即调用一个lambda函数,这个函数中定义的任何变量,都 ...

  5. [App Store Connect帮助]二、 添加、编辑和删除用户(5)创建一个沙盒测试员帐户

    如果您的 App 使用了 App 内购买项目或 Apple Pay,您可以在 App Store Connect 中创建沙盒测试员帐户,以便您向用户提供该 App 前,可以使用该帐户在测试环境中运行您 ...

  6. redis取经之路

    redis基本数据结构 Redis使用的是自己构建的简单动态字符串(SDS)[simple dynamic string,SDS]的抽象类型,并将SDS用做Rdis的默认字符串表示 redis> ...

  7. quill支持json吗

    RT quill目前的驱动(2.4.2版本)不支持json,等待作者更新版本吧

  8. Codeforces 903G 巧妙的线段树

    思路: 巧妙的线段树 想方法将网络流往数据结构方向转化 http://www.cnblogs.com/yyf0309/p/8724558.html //By SiriusRen #include &l ...

  9. [Luogu 1052] noip 05 过河

    [Luogu 1052] noip 05 过河 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是 ...

  10. oracle 行转列函数pivot和unpivot

    今天接到业务部门的一个需求,需要对同一公司的不同财务指标进行排序,需要用到oracle的行转列函数unpivot. 财务报表的表结构为: 要实现业务部门的排序筛选功能,需要首先将行数据转为列数据: 使 ...