Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help on Java @Test public void testSumOfDistinctCubes() { // Explicit test cases String b1 = [4, 3]; assertEquals(b1, P2J6.sumOfDistinctCubes(91).toString()); String b2 = [5]; assertEquals(b2,

Need help on Java

image text in transcribed

@Test public void testSumOfDistinctCubes() {
// Explicit test cases
String b1 = "[4, 3]";
assertEquals(b1, P2J6.sumOfDistinctCubes(91).toString());
String b2 = "[5]";
assertEquals(b2, P2J6.sumOfDistinctCubes(125).toString());
String b3 = "[4, 3, 2]";
assertEquals(b3, P2J6.sumOfDistinctCubes(99).toString());
String b4 = "[7, 2]";
assertEquals(b4, P2J6.sumOfDistinctCubes(351).toString());
String b5 = "[11, 4]";
assertEquals(b5, P2J6.sumOfDistinctCubes(1395).toString());
String b6 = "[]";
assertEquals(b6, P2J6.sumOfDistinctCubes(2020).toString());
// Pseudorandom fuzz tester
CRC32 check = new CRC32();
Random rng = new Random(SEED);
int c = 1, step = 2, next = 10;
while(c > 0) {
List result = P2J6.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());
}

Make sure can pass the tester, thanks. I will vote up.

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, n=1456 can be expressed as sum of two cubes 113 + 53. This method should return the list of these distinct integers as an object of some subtype of List that prints out as [11, 5], the elements listed in descending order. If there is no way to break the given integer n 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 follows the same principle for the remaining elements to 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 the list [9, 7], even though 93 + 73 = 1072 just as well. This constraint may initially look and sound scary, but you can just fuhgettaboutit, since its enforcement is trivial by arranging the loop inside the recursive method to scan the current possibilities from highest to lowest. You should again use a private helper method that accepts additional parameters that were not present in the public method. sumOfDistinctCubes (int n, int C, private static boolean LinkedList soFar) This helper method receives the two extra recursion parameters c and soFar from the original method. The parameter c contains the highest integer that you are still allowed to use. Initially this should equal the largest possible integer whose cube is less than or equal to n, easily found with a while-loop. The parameter sofar contains the list of numbers that have already been taken into the list of cubes so that they will be available once the recursion has returned. 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 could use, so you simply return false. Otherwise, try taking c into the sum, remembering also to add 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, its 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 be read from the list soFar. 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, n=1456 can be expressed as sum of two cubes 113 + 53. This method should return the list of these distinct integers as an object of some subtype of List that prints out as [11, 5], the elements listed in descending order. If there is no way to break the given integer n 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 follows the same principle for the remaining elements to 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 the list [9, 7], even though 93 + 73 = 1072 just as well. This constraint may initially look and sound scary, but you can just fuhgettaboutit, since its enforcement is trivial by arranging the loop inside the recursive method to scan the current possibilities from highest to lowest. You should again use a private helper method that accepts additional parameters that were not present in the public method. sumOfDistinctCubes (int n, int C, private static boolean LinkedList soFar) This helper method receives the two extra recursion parameters c and soFar from the original method. The parameter c contains the highest integer that you are still allowed to use. Initially this should equal the largest possible integer whose cube is less than or equal to n, easily found with a while-loop. The parameter sofar contains the list of numbers that have already been taken into the list of cubes so that they will be available once the recursion has returned. 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 could use, so you simply return false. Otherwise, try taking c into the sum, remembering also to add 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, its 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 be read from the list soFar

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago