Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 27, 2018 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/c2785d6995fc79dea3ed0efa63d24df3 to your computer and use it in GitHub Desktop.
Save thmain/c2785d6995fc79dea3ed0efa63d24df3 to your computer and use it in GitHub Desktop.
public class SmallSquare {
public void solve(int n) {
int options = (int) Math.sqrt(n);
//solve using recursion
System.out.println("Minimum Numbers are Required Whose Square Sum is Equal To a "+n+": " + solveRecursively(n, options));
}
public int solveRecursively(int n, int options) {
if (n <= 0) {
return 0;
}
int min = solveRecursively(n - 1 * 1, options);
for (int i = 2; i <= options; i++) {
if (n >= i * i) {
min = Math.min(min, solveRecursively(n - i * i, options));
}
}
return min + 1;
}
public static void main(String[] args) {
int N = 12;
SmallSquare s = new SmallSquare();
s.solve(N);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment