id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

frew@mclean.com recursion-2point5 > countSquares
prev  |  next  |  chance

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)

public int countSquares(int n, int index){ }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 400

Copyright Nick Parlante 2017 - privacy