about | help | code help+videos | done | prefs |
Write a method that accepts a positive integer parameter called 'num'. The method returns how many "steps" are needed to start at 'num' and get to 1 by following this recursive procedure: If you can divide num by 2 (and get an integer result), do that (divide num by 2). If you can't divide num by 2, subtract 1 instead. For example, 15 does not divide evenly by 2, so subtract 1 instead. Now we have 14 and have used one step. 14 DOES divide evenly by 2, so do that to get 7, which is the second step. Next we would subtract 1 to get 6, using our third step. 6/2=3 for our fourth step. 3-1=2 for our fifth step. 2/2=1 for our sixth step. So we were able to reach 1 in 6 steps, so we return a 6. Obviously there is a recursive way to solve this problem. There may also be some interesting non-recursive ways. Recursion is ALLOWED but NOT REQUIRED. quiz2020_09_21_HL_recursiveMath(200) → 9 quiz2020_09_21_HL_recursiveMath(4096) → 12 quiz2020_09_21_HL_recursiveMath(4097) → 13 ...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: 410
Copyright Nick Parlante 2017 - privacy