我正在處理一些歐拉項目問題,需要一些幫助來了解我找到的解決方案。無法找出設置值的位置
我的問題是:在哪裏heck是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;
}
}
內建的'EnumerableRange'與'int'一起使用,而不是'long' - 也許這就是原因。 – Oded
yes - good catch – BrokenGlass