about | help | code help+videos | done | prefs |
Write a method that returns the number of steps required to reach a value of 1 by following this process: If the value of 'num' is evenly divisible by 'divBy' then get a new number by dividing 'num' by 'divBy'. If the value of 'num' is NOT evenly divisible by 'divBy' then multiple 'num' by one more than 'divBy' and then add one to get your new number. For example, if 'num' is 7 and 'divBy' is 3, since 7 is not evenly divisible by 3, instead multiply 7 by 4 (one more than 3) then add one to that result to get your new number. So the new number would by 7*4+1=29. Repeat this process with your new number until you get a result of 1. Return the total number of steps required in this process. This is a good problem to solve using recursion, but recursion is not required (if you really want to try to do it a more difficult way!) Please note that some of these results may not be the "real" results due to wrap-around of the long integers. Long was used instead of int in order to reduce this wrap-around problem. However, if your algorithm is correct it will agree with my results even if those do not always match the "real" results. feb26_2016_HL_crazySequence(5, 3) → 186 feb26_2016_HL_crazySequence(6, 2) → 8 feb26_2016_HL_crazySequence(2, 2) → 1 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 420
Copyright Nick Parlante 2017 - privacy