Hailstone Numbers

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Pick a positive integer n. If it is odd, multiply it by three and then add one. If n is even, divide it by two. The positive integer obtained is the new n, and this is repeated until the number becomes 1. Given the value of n, with 1 \le n< 2^{31}, determine the number of operations before n becomes 1.

Sample Input

3

Sample Output

7

Explanation

n will become 10, 5, 16, 8, 4, 2, then 1, which is a total of 7 operations.


Comments


  • 4
    jason6  commented on Oct. 27, 2018, 6:06 p.m.

    Collatz Conjecture!


    • -1
      SeanJxie  commented on Dec. 18, 2019, 10:04 a.m.

      Someone has been watching NumberPhile!


  • 6
    aeternalis1  commented on Nov. 18, 2017, 10:14 a.m. edited

    For the full explanation:

    1. Stop asking for help in comments.

    2. Go to https://slack.dmoj.ca/ and ask for help in the #help channel.