Revision b9f793d9-8c0c-4f03-8dbc-30e819b51ca1 - Code Review Stack Exchange

##Problem statement:
A Pythagorean triplet is a set of three natural numbers, A < B < C, for which, A² + B² = C².
Given a number N, check if there exists any Pythagorean triplet for which A + B + C = N.
 
 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;
	}
 }

I was working on [this][1] program in ProjectEuler and Hackerrank consecutively. It ran perfectly for the input of 1000 and got accepted in Project Euler. But it is giving **Terminated Due to Timeout** error in most of the cases in Hackerrank.

How can I *optimize* it more to evade this error?


 [1]: https://www.hackerrank.com/contests/projecteuler/challenges/euler009/problem

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