Statistics

Problem Statement for "Collatz"

Problem Statement

The Collatz Problem asks whether the following function will reach 1 for all positive k if we keep applying it:
      { if k is odd    :   3*k +1 
f(k)= {
      { if k is even   :    k/2
For example, lets say you were given k = 20:
f(20) = 10     First application
f(10) = 5      Second application
f(5)  = 16     Third application
f(16) = 8      Fourth application
f(8)  = 4      Fifth application
f(4)  = 2      Sixth application
f(2)  = 1      Seventh application

So for k = 20 this function required 7 applications of the function to reach 1. Given an int k return the number of applications required to reach 1. Topcoder has ensured that the number of applications will be less than 1000 and that any temporary value will not overflow a 32-bit integer.

Definition

Class:
Collatz
Method:
numSteps
Parameters:
int
Returns:
int
Method signature:
int numSteps(int k)
(be sure your method is public)

Constraints

  • k will be between 2 and 100000 inclusive.
  • The return value will be less than 1000.

Examples

  1. 20

    Returns: 7

    The example from above

  2. 13

    Returns: 9

  3. 1000

    Returns: 111

  4. 99999

    Returns: 226

  5. 9999

    Returns: 91

  6. 77031

    Returns: 350

  7. 99999

    Returns: 226

  8. 2

    Returns: 1

    Just a single application required

  9. 5

    Returns: 5

    5*3+1 = 16 16/2 = 8 8/2 = 4 4/2 = 2 2/2 = 1

  10. 734

    Returns: 46

  11. 10992

    Returns: 42

  12. 99999

    Returns: 226

  13. 100000

    Returns: 128

  14. 73742

    Returns: 94

  15. 24533

    Returns: 51

  16. 34555

    Returns: 248

  17. 23456

    Returns: 100

  18. 98765

    Returns: 53

  19. 18283

    Returns: 92

  20. 77671

    Returns: 231

  21. 8

    Returns: 3

  22. 10000

    Returns: 29

  23. 8

    Returns: 3

  24. 10000

    Returns: 29

  25. 8

    Returns: 3

  26. 10000

    Returns: 29


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: