1

我正在尝试创建一个方法来检查一个数字是否丰富(他所有适当除数的总和大于数字本身),但我在1.ToInc(28123, 1)代码行中收到一个错误,上面写着:“并非所有代码路径都返回一个类型为“的匿名方法中的值System.Func<int,bool>。我相信这是指我的isAbundant方法。什么isAbundant不返回值?

public void Solve ()
{
    //Check if number is abundant -> number < sum of proper divisors. memoize this
    Func<int, bool> isAbundant = x =>
    {
        return x < ProperDivisors (x).Select(x => x).Sum () && x >= 0;
    };

        isAbundant = isAbundant.Memoize ();

        //make a sequence of all these numbers for readability
        var seq = 12.ToInc (28123, 1).TakeWhile (x => isAbundant (x));

                    // take all the numbers between 13 and 28123 take while (from 12 to number / 2 from sequence) check if number - item from sequence isn't abundant
        1.ToInc (28123, 1).TakeWhile (x => {
            foreach (var i in seq){
                if (i < x/2){
                    if (isAbundant (x - i))
                        return false;
                    else
                        return true;
                }
            }
        })
        // Sum
        .Sum ().Display ();
    }
}
4

1 回答 1

2

这实际上是指您的 lambda :

.TakeWhile (x => {
        foreach (var i in seq){
            if (i < x/2){
                if (isAbundant (x - i))
                    return false;
                else
                    return true;
            }
        }

        // If there are no elements in seq, you'll reach here, and never return a value!
        // Add something here, ie:
        return false;
    })
于 2013-07-31T17:13:06.120 回答