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...
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
Building Java Programs: A Back to Basics Approach (4th Edition)
- 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…arrow_forwardWrite a recursive method that gets two parameters as input: an array of integers called nums and an integer called index. The purpose of this method is to return the following: nums[index] * nums[index+ 1] * nums[index+ 2] * .. nums[nums.length-1]. Do not use loops. (java code)arrow_forwardWrite a recursive method that gets three parameters as input: an array of integers called nums, an integer called index, and an integer called value. The purpose of this method is to count how many times value appears in the array starting at nums[index]. Do not use loops. (Java code)arrow_forward
- Do not use static variables to implement recursive methods. USING JAVA: // P6 public static int countSubstrings(String s1, String s2) { } Write a recursive method countSubstrings that counts the number of non-overlapping occurrences of a string s2 in a string s1. Use the method indexOf() from String class. As an example, with s1=”abab” and s2=”ab”, countSubstrings returns 2. With s1=”aaabbaa” and s2=”aa”, countSubstrings returns 2. With s1=”aabbaa” and s2=”AA”, countSubStrings returns 0. Show the output on the following strings: s1 = “Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.…arrow_forwardWrite a recursive method called doubleDigits that accepts an integer n as a parameter and returns the integer obtained by replacing every digit of n with two of that digit. For example, doubleDigits(348) should return 334488. The call doubleDigits(0) should return 0. Calling doubleDigits on a negative number should return the negation of calling doubleDigits on the corresponding positive number; for example, doubleDigits(–789) should return –778899.arrow_forwardYou will be given an array A of length n. Write a method to calculate the following value of X using recursion. X = Ao x 7° + A1 x 7' + Az × 7² +. +An-1 x 7"-1 • The recursion can have any number of parameters. You can't use loops or math.pow() or a separate recursive function to calculate 7". The return type should be integer. • The range for n is 1arrow_forwardWrite a RECURSIVE method called “sequence” that takes a single int parameter (n) and returns the int value of the nth element of the sequence S = 2, 4, 6, 12, 22, 40, 74, 136, 250, 460, … Where S is defined by the recursive formula: For n >= 0S(0) = 2; // Base case 1S(1) = 4; // Base case 2S(2) = 6; // Base case 3S(N) = 2 * ( S(N-1)/2 + S(N-2)/2 + S(N-3)/2)arrow_forwardWe want to create a recursive method called numbers. This method is suppose to create the following sample output numbers(10) should display 10 9 8 7 6 5 4 3 2 1 0 numbers(5) should display 5 4 3 2 1 0 Fill in the blank so that the method generates the desired output public static void numbers(int a) { if (_1__ ___2___ __3___) <----- base case return; System.out.print(___4___ + " "); ___5___(___6___ ___7___ _____8____); <--- recursive case }arrow_forwardWrite a static recursive method called mrecursiv that displays all of the permutations of the characters in a string passed to the method as its argument. For example, the character sequence abc has the following permutations: acb, bac, bca, cab, cba. Then Write a static method called getInput that get an input string from the user and passed it to the mrecursiv method written above in a method call.arrow_forwardWrite a recursive method called reverseString() that takes in a string as a parameter and returns the string in reversed order. The main method is provided to read a string from the user and call the reverseString() method. Use Java. Ex: If the input of the program is: Hello the reverseString() method returns and the program outputs: Reverse of "Hello" is "olleH". Ex: If the input of the program is: Hello, world! the reverseString() method returns and the program outputs: Reverse of "Hello, world!" is "!dlrow ,olleH". Hint: Move the first character to the end of the returning string and pass the remaining sub-string to the next reverseString() method call. import java.util.Scanner; public class LabProgram { /* TODO: Write recursive reverseString() method here. */public static void main(String[] args) {Scanner scnr = new Scanner(System.in);String input, result;input = scnr.nextLine();result = reverseString(input); System.out.printf("Reverse of \"%s\" is \"%s\".", input, result);}}arrow_forwardWrite a recursive method that takes as parameter as array of type double (num) and the length of the array (len). The method should display the square root of numbers that are greater than 10.0 in the array. The method prototype is: public static void display(double [] num, int len); You may use sqrt method that is defined in the Math class.arrow_forwardWrite a recursive method that returns thelargest integer in an array. Write a test program that prompts the user to enter alist of eight integers and displays the largest element.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education