Problem statement:
##Problem statement: AA Pythagorean triplet is a set of three natural numbers, A < B < C, for which, A2 + B2 = C2. Given a number N, check if there exists any Pythagorean triplet for which A + B + C = N.
##Problem statement: A Pythagorean triplet is a set of three natural numbers, A < B < C, for which, A2 + B2 = C2. Given a number N, check if there exists any Pythagorean triplet for which A + B + C = N.
Problem statement:
A Pythagorean triplet is a set of three natural numbers, A < B < C, for which, A2 + B2 = C2. Given a number N, check if there exists any Pythagorean triplet for which A + B + C = N.
Project Euler #9: SpecialFinding Pythagorean triplet - code optimizationtriplets with a given sum
public class SpecialPythagoreanTriplet {
public class SpecialPythagoreanTriplet {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
int n=0, i=0, j=0, a=0, b=0, c=0;
while(t!=0) {
n = scan.nextInt();
a=0; b=0; c=0;
for(i=1; i<n; i++)
for(j=(i+1); j<=n; j++) {
//These two loops run to take a pair of numbers. j=(i+1) to avoid repetitions in the future.
if(isSquare((i*i) + (j*j))) //check if a number is a square
if(((int)Math.sqrt((i*i)+(j*j)) + i + j) == n) {
a = i;
b = j;
c = (int)Math.sqrt((i*i)+(j*j));
}
}
if(c==0)
System.out.println(-1);
else
System.out.println(a*b*c);
t--;
}
scan.close();
}
static boolean isSquare(double t) {
int a = (int)Math.sqrt(t);
if(a*a == t)
return true;
else
return false;
}
}
public class SpecialPythagoreanTriplet {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
int n=0, i=0, j=0, a=0, b=0, c=0;
while(t!=0) {
n = scan.nextInt();
a=0; b=0; c=0;
for(i=1; i<n; i++)
for(j=(i+1); j<=n; j++) {
//These two loops run to take a pair of numbers. j=(i+1) to avoid repetitions in the future.
if(isSquare((i*i) + (j*j))) //check if a number is a square
if(((int)Math.sqrt((i*i)+(j*j)) + i + j) == n) {
a = i;
b = j;
c = (int)Math.sqrt((i*i)+(j*j));
}
}
if(c==0)
System.out.println(-1);
else
System.out.println(a*b*c);
t--;
}
scan.close();
}
static boolean isSquare(double t) {
int a = (int)Math.sqrt(t);
if(a*a == t)
return true;
else
return false;
}
}
public class SpecialPythagoreanTriplet {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
int n=0, i=0, j=0, a=0, b=0, c=0;
while(t!=0) {
n = scan.nextInt();
a=0; b=0; c=0;
for(i=1; i<n; i++)
for(j=(i+1); j<=n; j++) {
//These two loops run to take a pair of numbers. j=(i+1) to avoid repetitions in the future.
if(isSquare((i*i) + (j*j))) //check if a number is a square
if(((int)Math.sqrt((i*i)+(j*j)) + i + j) == n) {
a = i;
b = j;
c = (int)Math.sqrt((i*i)+(j*j));
}
}
if(c==0)
System.out.println(-1);
else
System.out.println(a*b*c);
t--;
}
scan.close();
}
static boolean isSquare(double t) {
int a = (int)Math.sqrt(t);
if(a*a == t)
return true;
else
return false;
}
}
- 87.8k
- 14
- 104
- 325