Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- import java.io.*;
- import java.math.*;
- public class Solution {
- public static void main(String[] args) {
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- try{
- //precomputation
- int save[] = new int[3001];
- for(int i=1; i<3001; i++){
- int iSqr = i*i;
- for(int j=i+1; j<3001; j++){
- double x = Math.sqrt(iSqr + j*j);
- if(x == Math.floor(x)){
- int sum = i+j+(int)x;
- int product = i*j*(int)x;
- if((sum < 3001) && (product > save[sum]))
- save[sum] = product;
- }
- }
- }
- int t = Integer.parseInt(stdin.readLine());
- while(t-- > 0){
- int n = Integer.parseInt(stdin.readLine());
- if(save[n] == 0)
- System.out.println(-1);
- else
- System.out.println(save[n]);
- }
- }
- catch(Exception e){
- }
- }
- }
Add Comment
Please, Sign In to add comment