Problem 21
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的更多相关文章
- (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 ...
- [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 ...
- uoj problem 21 缩进优化
题目: 小O是一个热爱短代码的选手.在缩代码方面,他是一位身经百战的老手.世界各地的OJ上,很多题的最短解答排行榜都有他的身影.这令他感到十分愉悦. 最近,他突然发现,很多时候自己的程序明明看起来比别 ...
- ●UOJ 21 缩进优化
题链: http://uoj.ac/problem/21 题解: ...技巧题吧 先看看题目让求什么: 令$F(x)=\sum_{i=1}^{n}(\lfloor a[i]/x \rfloor +a[ ...
- 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 ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
- electrica writeup
关于 caesum.com 网上上的题目,分类有Sokoban,Ciphers,Maths,Executables,Programming,Steganography,Misc.题目有点难度,在努力奋 ...
- 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 ] 给 ...
- [xjtu21]wmq的午餐 计数问题
http://oj.xjtuacm.com/problem/21/ 对13进行分析,每种价格出现的次数: $(C_m^1 + C_m^2 + ... + C_m^m)(C_{n - m}^0 + C_ ...
随机推荐
- 专訪印度电商Snapdeal CEO:学阿里还是京东
[摘要]印度的互联网正成资本关注下一个投资焦点,也可能成中国互联网企业走向海外的桥头堡.为此.腾讯科技最近将推出走近印度"硅谷"系列文章,帮助大家了解印度互联网. 腾讯科技与Sna ...
- SmartSchool CC校友录V8(毕业入世版)
SmartSchool CC校友录V8(毕业入世版) 使用说明 CC校友录V8(毕业入世版) 主要面向毕业后在某城市工作的校友,给大家构建一个充分交流的平台,“人脉”积累是本软件的功能特色,为此淡化了 ...
- 逆向工程之App脱壳
http://www.cnblogs.com/ludashi/p/5725743.html iOS逆向工程之App脱壳 本篇博客以微信为例,给微信脱壳."砸壳"在iOS逆向工程中是 ...
- hbase查询_Phoenix及hbase repl命令行两种方式
一.Phoenix(jdbc)登陆 1.cd /home/mr/phoenix/bin(此路径每个环境里面有可能不一样)2../sqlline.py localhost 二.shell repl Hb ...
- js添加页面元素
js动态创建html元素需要使用到下面这些常见的js方法. getElementById();//返回带有指定 ID 的元素. getElementsByTagName();//返回包含带有指定标签名 ...
- Codeforces--400A--Inna and Choose Options(模拟水题)
Inna and Choose Options Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- FSDataInputStream对象 读取数据
- 浅谈IO优化
三层结构 磁盘(存储).VM(卷管理)和文件系统.专有名词不好理解,打个比方说:磁盘就相当于一块待用的空地:LVM相当于空地上的围墙(把空地划分成多个部分):文件系统则相当于每块空地上建的楼房(决定了 ...
- 根据JSON创建对应的HIVE表
本文提供一种用SCALA把JSON串转换为HIVE表的方法,由于比较简单,只贴代码,不做解释.有问题可以留言探讨 package com.gabry.hiveimport org.json4s._im ...
- javascript中window,document,body的解释
解释javascript中window,document,body的区别: window对象表示浏览器中打开的窗口,即是一个浏览器窗口只有一个window对象. document对象是载入浏览器的ht ...