13195の素因数は5、7、13、29である。 600851475143の素因数のうち最大のものを求めよ。
using System;
class Program
{
//const long TargetVal = 13195;
const long TargetVal = 600851475143;
static void Main()
{
long CopiedVal = TargetVal;
long KariMax = 1;
for (long I = 2; I <= CopiedVal; I++) {
while (CopiedVal % I == 0) {
KariMax = I;
CopiedVal /= I;
}
}
Console.WriteLine("最大の素因数={0}", KariMax);
}
}
最大の素因数=6857
2から順に試し割りしてます。