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

 

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

Write a method that returns the number of "steps" needed to get to 1 from the positive input parameter 'num'. At each stage, each "step" proceeds in a particular order: First, if 'num' is even, divide num by 2. Second, if 'num' is a multiple of three, divide num by 3. Finally, if neither of these applies, subtract one from num. Each of these steps leads to a new value of num, and the steps continue until you reach a value of 1. Return the number of steps needed. For example: if 'num'=17, 5 steps are required. 17 is neither even nor a multiple of 3, so subtract 1 to get 16 (that is the first step). 16 is even so divide by 2 to get 8 (that is the second step). 8 is even so divide by 2 to get 4 (that is the third step). 4 is even, so divide by 2 to get 2 (that is the fourth step). 2 is even, so divide by 2 to get 1 (that is the fifth and final step). Look at the test data for additional examples.


test2020_10_29_BONUS_steps(1) → 0
test2020_10_29_BONUS_steps(44) → 7
test2020_10_29_BONUS_steps(4782969) → 14

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

public int test2020_10_29_BONUS_steps(int num) { }

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