static void findSquaredSet(int n) {
System.out.println("present a givan num as sum of minimum required perfect squares: " + n);
int j = (int) Math.floor(Math.sqrt(n));
Stack stack = new Stack();
int dif = n;
while (j >= 1 && dif > 0) {
//dif=dif-j*j;
if (dif != 3 && dif != 8 && dif < j * j) {
j--;
} else {
if (dif == 3) {
stack.push(1);
stack.push(1);
stack.push(1);
break;
} else if (dif == 8) {
stack.push(2);
stack.push(2);
break;
} else {
if (dif - j * j > 0) {
dif = dif - j * j;
stack.push(j);
}
j--;
}
}
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + "^2+ ");
}
System.out.println("=" + n);
}
No comments:
Post a Comment