Last active
May 27, 2018 04:50
-
-
Save thmain/c2785d6995fc79dea3ed0efa63d24df3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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