Java和毕达哥拉斯三元数组

来源:百度知道 编辑:UC知道 时间:2024/06/18 00:01:12
怎样使用这个代码来解决毕达哥拉斯三元数组
要求:询问用户输入一个最大斜边长度的数字,通过console,指派final variable

例如:20
显示出 3,4,5
5,12,13,
8,15, 17
这几组可能的数值,如此类推。

static boolean isPrime ( int number ) {

/* This method tests to see if number is a prime number.
If so, it will return true. Otherwise it will return
false. */
for ( int j = 2; j * j <= number; j++) {
if ( number % j == 0 )
return false;
}//for
return true;
}//isPrime

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
final long n = sc.nextLong();
long a,b,c;
List coprime = new ArrayList();
for(c=0;c<=n;c++)
{
for(a=0;a<c;a++)
{
for(b=a;b<c;b++)
{
if((a*a+b*b)==(c*c))
{
if(check(a)||check(b)||check(c)) coprime.add(a+","+b+","+c);
}
else if((a*a+b*b)>(c*c)) break;
}
}
}
System.out.println("The total number of primitive Pythagorean triple is "+coprime.size());
for(String item:(ArrayList<String>)coprime) System.out.println(item);
}

public static boolean check(long x)
{
for (int y = 2; y <= Math.sqrt(x); y++)
{