📅  最后修改于: 2022-03-11 14:48:46.457000             🧑  作者: Mango
static long LargestFactor(long n)
{
long lastFactor;
if (n%2==0)
{
lastFactor = 2;
n/=2;
while (n % 2 == 0)
n/=2;
}
else
{
lastFactor = 1;
}
long factor = 3;
long maxFactor = Convert.ToInt64(Math.Sqrt(n));
while (n > 1 && factor <= maxFactor)
{
if(n % factor == 0)
{
n/=factor;
lastFactor=factor;
while (n % factor == 0)
n/= factor;
maxFactor = Convert.ToInt64(Math.Sqrt(n));
}
factor += 2;
}
if (n == 1)
return lastFactor;
else
return n;
}