为什么这个Sage程序不能正常工作(projecteuler 23)?

2024-07-03 06:54:36 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我对欧拉计划问题23的解答,它是:

“一个完全数是一个数,它的固有因子之和正好等于这个数。例如,28的真因子之和是1+2+4+7+14=28,这意味着28是一个完美的数字。你知道吗

如果一个数n的真因子之和小于n,则称为亏数;如果这个数n的真因子之和大于n,则称为丰数

因为12是最小的富足数,1+2+3+4+6=16,所以可以写成两个富足数之和的最小数是24。通过数学分析可以看出,所有大于28123的整数都可以写成两个富足数的和。然而,即使已知不能表示为两个充裕数之和的最大数小于这个极限,通过分析也不能进一步降低这个上限。你知道吗

求所有不能写成两个充裕数之和的正整数之和。”

我已经写了这段代码,但出于某种原因,它给了我4190404,这是根据项目Euler网站是不正确的。你知道吗

import numpy

def lowfactor(n):
    factors = [i for i in xrange(2, ceil(sqrt(n))) if n % i == 0]

    return list(numpy.unique(factors))

def factor(n):
    low = lowfactor(n)
    factors = [n / i for i in low]
    factors.reverse()
    factors = factors + low

    return factors

def isAbundant(n):
    factors = factor(n)
    factorSum = sum(factors)

    return factorSum > n

abundants = [i for i in xrange(28124) if isAbundant(i)]

sums = map(lambda n: False, range(28124))

for a in xrange(0, len(abundants)):
    for b in xrange(a, len(abundants)):
        if (abundants[a] + abundants[b]) < 28124:
            sums[abundants[a] + abundants[b]] = True

notsums = []
for i in xrange(28124):
    if not sums[i]:
        notsums.append(i)

sumofnotsums = sum(notsums)

print(sumofnotsums)

Tags: innumpyforreturnifdeflowfactors