{20,48,52}, {24,45,51}, {30,40,50}
對於1000以下的p中,哪一個能夠產生最多的解?
public class Problem39
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
System.out.print("answer: ");
howmany();
long end = System.currentTimeMillis();
System.out.print("time: ");
System.out.println(end - start);
}
static void howmany()
{
int max = 0;
int answer = 0;
for (int p = 4; p < 1000; p++)
{
int t = ab(p);
if (max < t)
{
answer = p;
max = t;
}
}
System.out.println(answer);
}
static int ab(int p)
{
int cishu = 0;
for (int a = 1; a < p; a++)
{
for (int b = 1; b < p; b++)
{
int c = p - a - b;
if (c < 0)
{
break;
}
if(a * a + b * b == c * c)
{
cishu++;
}
}
}
return cishu;
}
}
answer: 840
time: 250