about | help | code help+videos | done | prefs |
Write a recursive method called countSquares to find all ways to express an integer as a sum of squares of unique positive integers. For example, 10 = 1^2 + 3^2, Hence total 1 possibility. 100 = 10^2 OR 6^2 + 8^2 OR 1^2 + 3^2 + 4^2 + 5^2 + 7^2 Hence total 3 possibilities. Some numbers (such as 128 or 0) cannot be represented as a sum of squares, in which case your method should return 0. Keep in mind that the sum has to be formed with unique integers. Otherwise you could always find a solution by adding together 1^2 until you got to whatever number you are working with. As with any backtracking problem, this one amounts to a set of choices, one for each integer whose square might or might not be part of your sum. countSquares(200, 1) → 9 countSquares(0, 1) → 0 countSquares(128, 1) → 0 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 400
Copyright Nick Parlante 2017 - privacy