我实际上是在练习Euler项目,以提高我在C#中的技能,并且在问题3中,我写了下面的i Presence presence,但它不起作用,因为它给了我错误的结果。问题链接是这里
你能告诉我为什么吗?
using System;
using System.Linq;
namespace ConsoleApplication22
{
class Program
{
static void Main(string[] args)
{
int[] rep = new int[35];
int index = 0;
long nb = 600851475143;
int divider = 2;
while (divider < nb / 2)
{
while (nb % divider == 0)
{
nb /= 2;
rep[index] = divider;
index += 1;
}
divider += 1;
}
Console.WriteLine("The annswer is : " + rep.Max());
Console.Read();
}
}
您应该除以divider
而不是2
。另外,第一个while
应该转到nb
,因为您始终使nb
较小。
static void Main(string[] args)
{
int[] rep = new int[35];
int index = 0;
long nb = 600851475143;
int divider = 2;
while (divider <= nb)
{
while (nb % divider == 0)
{
nb /= divider;
rep[index] = divider;
index += 1;
}
divider += 1;
}
Console.WriteLine("The answer is : " + rep.Max());
Console.Read();
}