Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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.

edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Project Euler #9: SpecialFinding Pythagorean triplet - code optimizationtriplets with a given sum

update indentation
Source Link

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;
}
}
We use TLE /instead of/ Performance; minor grammar/formatting fixes
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
added problem satement, and improved header line.
Source Link
Loading
edited tags
Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Source Link
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /