id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

jebbert@volusia.k12.fl.us > test2024_12_12_HL_absDiffAdMult
prev  |  next  |  chance

The additive persistence of an integer, 'num', is the number of times you have to replace 'num' with the sum of its digits until 'num' becomes a single digit integer. The multiplicative persistence of an integer, n, is the number of times you have to replace n with the product of its digits until n becomes a single digit integer. Examples: Additive Persistence additivePersistence(1679583) ➞ 3 // 1 + 6 + 7 + 9 + 5 + 8 + 3 = 39 // 3 + 9 = 12 // 1 + 2 = 3 // It takes 3 iterations to reach a single-digit number. additivePersistence(123456) ➞ 2 // 1 + 2 + 3 + 4 + 5 + 6 = 21 // 2 + 1 = 3 additivePersistence(6) ➞ 0 // Because 6 is already a single-digit integer. Examples: Multiplicative Persistence multiplicativePersistence(77) ➞ 4 // 7 x 7 = 49 // 4 x 9 = 36 // 3 x 6 = 18 // 1 x 8 = 8 // It takes 4 iterations to reach a single-digit number. multiplicativePersistence(123456) ➞ 2 // 1 x 2 x 3 x 4 x 5 x 6 = 720 // 7 x 2 x 0 = 0 multiplicativePersistence(4) ➞ 0 // Because 4 is already a single-digit integer. Suggestion: Create two helper methods as described above that take an integer 'num' as an argument and: - One of them returns the additive persistence of the input parameter. - The other one returns the multiplicative persistence of the input parameter. Alternative Suggestion: Create one helper method that does BOTH of these things at the same time (this can be more efficient, but also much more difficult). You may choose to use additional helper methods. Recursion is not required but may be helpful. Now that you have your two helper methods, you will write a method that returns the NUMBER within the range 'least'<=NUMBER<='greatest' that has the greatest absolute difference between the additive persistence of NUMBER compared to the multiplicative persistence of NUMBER. If there is a tie between different NUMBERS in the range that produce the same absolute difference, return the smallest of these NUMBERS. As a precondition, you may assume that 'least' and 'greatest' are both positive, and that 'least'<='greatest'. The test data might not really help you with this problem because there are so many different parts to it. It is hard to manually verify the test data! That is part of the challenge! IMPORTANT NOTE: I don't think there is an easy way to make this super-efficient, even if you use my (more difficult) alternate suggestion from above. That is OK. It should still work. However, SOMETIMES you might get time-out errors. Try running it again a few times. If you continue to get time-out errors, you probably have something wrong. However, if you ever get "all green", your code is probably fine and CodingBat is just being a little slow. This problem DOES require quite a bit of process time, so occasional time-out errors should be expected. Just make sure the LAST time you run it is "all green" and it will count as being solved!


test2024_12_12_HL_absDiffAdMult(8527, 9322) → 8618
test2024_12_12_HL_absDiffAdMult(42, 92) → 77
test2024_12_12_HL_absDiffAdMult(99, 209) → 177

...Save, Compile, Run (ctrl-enter)

public int test2024_12_12_HL_absDiffAdMult(int least, int greatest) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 290

Copyright Nick Parlante 2017 - privacy