您的位置:首页 > 编程语言 > Python开发

欧拉计划21

2016-11-07 11:44 120 查看
昨天困得不得了,忘记做了,还是发现用python写这种东西太方便了,再来试一下。

原题如下:

Amicable numbers


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.

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.

代码如下:

# -*- coding: UTF-8 -*-
def sum1(n):
sum = 0
for i in range(1,n/2 + 1):
if n%i == 0 :
#print i
sum+=i
#print sum
return sum

if __name__=="__main__":
sum = 0;
for i in range(1,10000 + 1):
test1 = sum1(i)
if( i == sum1(test1) and i != test1):
sum += i
print sum

结果为:  31626 !!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  欧拉计划 python