Question

public static List sumOfDistinctCubes(int n) Determine whether the given positive integer n can be expressed as...

public static List sumOfDistinctCubes(int n)

Determine whether the given positive integer n can be expressed as a sum of cubes of positive integers greater than zero so that all these integers are distinct. For example, the integer n = 1456 can be expressed as sum 11 3 + 5 3 . This method should return the list of these distinct integers as a list [11, 5] with the elements given in descending order. If n cannot be broken into a sum of distinct cubes, this method should return the empty list.

Many integers can be broken down into a sum of cubes in several different ways. This method must always return the breakdown that is lexicographically highest, that is, starts with the largest possible working value for the first element, and then follow the same principle for the remaining elements that break down the rest of the number into a list of distinct cubes. For example, when called with n = 1072, this method must return the list [10, 4, 2] instead of [9, 7], even though 9 3 + 7 3 = 1072.

Hint: the easiest way to implement this recursion is again to have a second private method

private static boolean sumOfDistinctCubes(int n, int c, LinkedList soFar)

that receives two extra parameters c and soFar from the original method. The parameter c gives you the highest number that you are still allowed to use (initially this should equal the largest possible integer whose cube is less than equal to n, easily found with a while-loop), and the parameter soFar contains the list of numbers that have already been taken in. The recursion has two base cases, one for success and one for failure. If n == 0, the problem is solved and you can just return true. If c == 0, there are no numbers remaining that you can use, and you simply return false. Otherwise, try taking c into the sum (also adding c to soFar) and recursively solve the problem for new parameters n-c*c*c and c-1. If that one was not a success, remove c from soFar, and try to recursively solve the problem without using c, which makes the parameters of the second recursive call to be n and c-1.

Note how, since this method is supposed to find only one solution, once the recursive call returns true, the caller can also immediately return true without exploring the remaining possibilities. The first success will therefore cause the entire recursion to immediately roll back all the way to the top level where the breakdown of n into distinct cubes can then be read from the list soFar.

Here is the tester for this method assuming it is in a class named Main:

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Random;

import java.io.*;
import java.util.*;
import java.util.zip.CRC32;

public class SumOfDistinctSquaresTest {

private static final int SEED = 12345;
  
@Test public void testSumOfDistinctCubes() {
CRC32 check = new CRC32();
Random rng = new Random(SEED);
int c = 1, step = 2, next = 10;
while(c > 0) {
List<Integer> result = Main.sumOfDistinctCubes(c);
check.update(result.toString().getBytes());
c += rng.nextInt(step) + 1;
if(c > next) {
next = 2 * next;
step = 2 * step;
}
}
assertEquals(4219145223L, check.getValue());
}
  
private String createString(String alphabet, Random rng, int n) {
String result = "";
for(int i = 0; i < n; i++) {
result += alphabet.charAt(rng.nextInt(alphabet.length()));
}
return result;
}
  
  
}

The program must pass the tester to work (it can't run for too long)

0 0
Add a comment Improve this question Transcribed image text
Answer #1


        private static boolean sumOfDistinctCubes(int n, int c, LinkedList<Integer> soFar) {
            if(n == 0) {
                return true;
            }
            if(c == 0) {
                return false;
            }
            soFar.add(c);
            boolean subProblem = sumOfDistinctCubes(n-c*c*c, c-1, soFar);
            if(subProblem) {
                return true;
            }
            
            // we were unable to solve.
            soFar.remove(new Integer(c));
            return sumOfDistinctCubes(n, c-1, soFar);
        }

        public static List<Integer> sumOfDistinctCubes(int c) {
            LinkedList<Integer> result = new LinkedList<Integer>();
            
            int a = (int) Math.pow(c, 1.0/3);
            
            sumOfDistinctCubes(c, a, result);
                    
            return result;
        }

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
public static List sumOfDistinctCubes(int n) Determine whether the given positive integer n can be expressed as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • static public int[] Question() // retrieve an integer 'n' from the console // the first input...

    static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...

  • Java, how would i do this public static void main(String[] args) { int n = 3;...

    Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...

  • public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array...

    public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] Parameters: start - starting integer numIterations - how long to compute the Collatz sequence for Returns: an array containing the Collatz...

  • collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an...

    collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] Parameters: start - starting integer numIterations - how long to compute the Collatz sequence for Returns: an array containing the...

  • public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array...

    public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] TESTING: collatz(7,3) should return {7, 22, 11, 34} collatz(6,0) should return {6} collatz(6, 5) should return {6, 3, 10, 5, 16,...

  • public static int Fibonacci(int n) This method receives an integer as a parameter and returns the...

    public static int Fibonacci(int n) This method receives an integer as a parameter and returns the index of n in the Fibonacci series. If n does not exist, the method returns -1. C#

  • Import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc...

    import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the value of k int k = Integer.parseInt(sc.nextLine());    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    System.out.println(findPairs(numbers, k)); }    //method that...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int...

    Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT