我正在处理一些 Project Euler 问题,需要一些帮助来理解我找到的解决方案。
我的问题是:X 在 SkipWhile 方法调用中设置在哪里?当我在运行时中断代码并单步执行到该点时,我从来没有看到为它设置了一个值。然而,代码将一直有效。我检查了 SkipWhile 的定义,也许我只是不明白调用中传递的参数如何满足 3 参数方法定义。Math.Pow 也是一样 - X 在哪里设置!?
public long FindGreatestPrimeFactor(long factorGreaterThan, long number)
{
long upperBound = (long)Math.Ceiling(Math.Sqrt(number));
// find next factor of number
long nextFactor = Range(factorGreaterThan + 1, upperBound)
.SkipWhile(x => number % x > 0).FirstOrDefault();
// if no other factor was found, then the number must be prime
if (nextFactor == 0)
{
return number;
}
else
{
// find the multiplicity of the factor
long multiplicity = Enumerable.Range(1, Int32.MaxValue)
.TakeWhile(x => number % (long)Math.Pow(nextFactor, x) == 0)
.Last();
long quotient = number / (long)Math.Pow(nextFactor, multiplicity);
if (quotient == 1)
{
return nextFactor;
}
else
{
return FindGreatestPrimeFactor(nextFactor, quotient);
}
}
}
private IEnumerable<long> Range(long first, long last)
{
for (long i = first; i <= last; i++)
{
yield return i;
}
}