^^ Will try out ur logic mate.
This works i guess. Algorithm isn't mine and i found it over web.
Check it out guys:
Code:
[B]public class lagrange{
public static void sum_of_squares(int n){
int t1, t2, t;
for (int i = (int) Math.sqrt(n / 4); i * i <= n; i++) {
t1 = n - i * i;
for (int j = (int) Math.sqrt(t1 / 3); j <= i && j * j <= t1; j++) {
t2 = t1 - j * j;
for (int k = (int) Math.sqrt(t2 / 2); k <= j && k * k <= t2; k++) {
t = (int) Math.sqrt(t2 - k * k);
if (t <= k && t * t == t2 - k * k) {
System.out.println("(" + i + "^2) + (" + j + "^2) + ("+ k + "^2) + ("+ t +"^2)");
}
}
}
}
}
public static void main (String [] args){
sum_of_squares(29);
}
}
[/B]
Pass any arguements into the sum_of_squares method. It also gives multiple possibilities. Numbers like 23 won't work coz it requires 5 terms
i.e 4^2 + 2^2+ 1^2+ 1^2+ 1^2
Algorithm for that is different. Interestingly, all counter variables used, yield the answer.