码迷,mamicode.com
首页 > 编程语言 > 详细

欧拉计划(python) problem 23

时间:2015-02-05 09:38:22      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

Non-abundant sums

Problem 23

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


Answer:
4179871
Completed on Wed, 4 Feb 2015, 13:58

Go to the thread for problem 23 in the forum.

python code:

import math
sqrt=math.sqrt


def func1(x):
    m=sqrt(x)
    if m-int(m)==0:
        k=1+int(m)
        for i in range(2,int(m)):
            if x%i==0:
                k+=i+int(x/i)
        return k
    else:
        k=1
        for i in range(2,int(m+1)):
            if x%i==0:
                k+=i+int(x/i)
        return k


dict={}
a=[]
for i in range(12,28123):
    if func1(i)>i:
        a.append(i)
        dict[i]=1


def func2(x):
    i=0
    k=len(a)
    while a[i]<x and i<k:
        if dict.get(x-a[i])==1:
            return 1
        i+=1
    return 0
result=0
for i in range(1,28123):
    if func2(i)==0:
        result+=i


print(result)


time:2s


------------------
祝身体健康,万事如意

华电北风吹

天津大学计算机科学与技术学院认知计算与应用重点实验室

天津市卫津路92号

邮编: 300072


欧拉计划(python) problem 23

标签:

原文地址:http://blog.csdn.net/zhangzhengyi03539/article/details/43495681

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!