Building Java Programs: A Back to Basics Approach (4th Edition)
Building Java Programs: A Back to Basics Approach (4th Edition)
4th Edition
ISBN: 9780134322766
Author: Stuart Reges, Marty Stepp
Publisher: PEARSON
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 12, Problem 22E

Explanation of Solution

Method definition:

/* Prints all ways to express n as a sum of squares of unique integers with precondition: n >= 0*/

//method definition

public static void printSquares(int n)

{

    //define the integer sets

    Set<Integer> chosen = new TreeSet<Integer>();

    //call the method explore

    explore(n, 1, chosen);

}

/* all ways are explored to form n as a sum of squares of integers starting

from the given min value and storing the chosen results with the given set*/

//method definition

private static void explore(int n, int min, Set<Integer> chosen)

{

    //validate the value of n to be zero

    if (n == 0)

    {

        //method call that is base case when sum has reached n

        printHelper(chosen);

    }

    else

    {

        // All possible combination are tried

        //validate the choices upto sqrt(n)

        int max = (int)Math...

Blurred answer
Students have asked these similar questions
Write a recursive method that gets three parameters as input: an array of integers called nums, an integer called index,and an integer called The purpose of this method is to return true if value is stored in the array starting at nums[index]. That is, you have to check if value is equal to nums[index] or nums[index +1] or nums[index +2 ] …. nums[nums.length -1]. Do not use loops.(java code)
Write a method printSquares that uses recursive backtracking to find all ways to express an integer as a sum of squares of unique positive integers. For example, the call of printSquares(200); should produce the following output:1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 8^2 + 9^21^2 + 2^2 + 3^2 + 4^2 + 7^2 + 11^21^2 + 2^2 + 5^2 + 7^2 + 11^21^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^21^2 + 3^2 + 4^2 + 5^2 + 7^2 + 10^22^2 + 4^2 + 6^2 + 12^22^2 + 14^23^2 + 5^2 + 6^2 + 7^2 + 9^26^2 + 8^2 + 10^2Some numbers (such as 128 or 0) cannot be represented as a sum of squares, in which case your method should produce no output. Keep in mind that the sum has to be formed with unique integers. Otherwise you could always find a solution by adding 1^2 together 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. In many of our backtracking problems we store the choices in…
The nth harmonic number is defined non-recursively as                                    H(n) = 1+1/2+1/3+1/4+⋯+1/n Come up with a recursive definition and use it to guide you to write a method definition for a double-valued method named “harmonic” that accepts an int parameter n and recursively calculates and returns the nth harmonic number. Write a test program that displays the harmonic numbers, H(n), for n = 1,2,3,⋯,10.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7); Author: CS Dojo;https://www.youtube.com/watch?v=D6xkbGLQesk;License: Standard YouTube License, CC-BY