Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code shown below fails the JUnit Test, Can you please help java.lang.AssertionError: Expected :916368163 Actual :3045627254 public static BigInteger sevenZero(int n) Since seven is a

code shown below fails the JUnit Test, Can you please help

java.lang.AssertionError: Expected :916368163 Actual :3045627254

public static BigInteger sevenZero(int n)

Since seven is a lucky number in the Western culture, whereas zero is what nobody wants to be, let us bring these two opposites together for a moment and look at positive integers that consist of some sequence of sevens, followed by some (possibly empty) sequence of zeros. For example, 0, 7, 77777, 7700000, 77777700, or 70000000000000000000000000000000000. All sevens must be in one consecutive bunch, followed by all the zeros in another consecutive bunch. This rule keeps the space of such numbers small enough to examine through a brute-force search, since there exist only n + 1 such numbers that have n digits. In the wonderful MIT OpenCourseWare online textbook "Mathematics for Computer Science" (PDF link to the 2018 version, for any of you who are interested in that sort of stuff), one of the examples of non-constructive proofs with the pigeonhole principle to establish the necessary existence of at least one mathematical object with the particular quality proved that for any positive integer n, there exists some seven-zero integer that is divisible by n. This integer made of sevens and zeroes can quickly get gargantuan even for relatively small values of n. For example, for n=12345, the smallest working seven-zero number that is divisible by n consists of whopping 822 copies of the digit seven, followed by a single zero digit tacked to the end. This method should find and return the smallest seven-zero integer that is divisible by the given n. The easiest way would be to use two nested loops. The outer while-loop iterates through all possible digit lengths (that is, how many digits the number contains) of the number. For each digit length, the inner for-loop iterates through all legal sequences of consecutive sevens followed by a sufficient number of zeros to make that number to have the desired digit length. Keep going up until you find the first seven-zero number exactly divisible by n. Since your loops iterate through the seven-zero numbers in ascending order, the number found this way must be the smallest such number that satisfies the problem requirement. Furthermore, you can utilize an additional theorem proven in the same book to speed up your search. Unless n is divisible by either 2 or 5, the smallest seven-zero number that is divisible by n is guaranteed to contain only sevens, and no zeros. This realization should speed up your search by at least an order of magnitude for such easy values of n, since the quadratic number of possibilities to examine is pruned down into a single linear branch that goes through the numbers 7, 77, 777, 7777, 77777... in search of the answer. On the other hand, numbers that contain the powers of two and five as their factors require seven-zero numbers whose tails of zero digits in the end grow along the exponents of these two little prime powers.

JUnit Test

@Test public void testSevenZero() { // Explicit test cases assertEquals(new BigInteger("7"), P2J5.sevenZero(1)); assertEquals(new BigInteger("7"), P2J5.sevenZero(7)); assertEquals(new BigInteger("7770"), P2J5.sevenZero(42)); assertEquals(new BigInteger("70"), P2J5.sevenZero(70)); assertEquals(new BigInteger("77"), P2J5.sevenZero(77)); assertEquals(new BigInteger("70000"), P2J5.sevenZero(16)); assertEquals(new BigInteger("7770"), P2J5.sevenZero(42)); assertEquals(new BigInteger("700"), P2J5.sevenZero(100)); assertEquals(new BigInteger("7000"), P2J5.sevenZero(125)); assertEquals(new BigInteger("7000"), P2J5.sevenZero(200)); assertEquals(new BigInteger("77700"), P2J5.sevenZero(300)); assertEquals(new BigInteger("70000"), P2J5.sevenZero(400)); assertEquals(new BigInteger("7000"), P2J5.sevenZero(500)); assertEquals(new BigInteger("70000"), P2J5.sevenZero(625)); assertEquals( new BigInteger("777777777777777777777777777777777777777777777777777777"), P2J5.sevenZero(513) ); assertEquals(new BigInteger("7777777770"), P2J5.sevenZero(666)); assertEquals(new BigInteger("777700"), P2J5.sevenZero(2020)); assertEquals(new BigInteger("70000000"), P2J5.sevenZero(625000)); // Systematic checksum tester CRC32 check = new CRC32(); Random rng = new Random(12345); int curr = 2; for(int i = 2; i < 400; i++) { BigInteger result = P2J5.sevenZero(curr); try { check.update(result.toString().getBytes("UTF-8")); } catch(UnsupportedEncodingException ignored) {} curr += rng.nextInt(5) + 1; } assertEquals(916368163L, check.getValue()); }

===================-----================

Reference code

public static BigInteger sevenZero(int n){ String seven = "7"; BigInteger num = new BigInteger(n+""); for(int i=0;i<1000;i++){ String zero=""; for(int j=0;j<100;j++){ String nx = seven+zero; BigInteger bn = new BigInteger(nx); BigInteger mod = bn.mod(num); if(mod.compareTo(new BigInteger("0"))==0) return bn; zero+="0"; } seven+="7"; } return new BigInteger("0");

Step by Step Solution

There are 3 Steps involved in it

Step: 1

The issue with your code is related to how youre constructing the BigInteger instances in the sevenZ... 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

Engineering economy

Authors: Leland Blank, Anthony Tarquin

7th Edition

9781259027406, 0073376302, 1259027406, 978-0073376301

More Books

Students also viewed these Programming questions