Pick a positive integer n. If it is odd, multiply it by three and then add one. Otherwise (if it is even), divide it by two. The positive integer obtained is the new n, and this procedure is repeated.

It is believed that n will eventually become 1 (this is called the Collatz conjecture.) Computers have checked that any value of n less than 5×1018 does, indeed, eventually become 1 if this procedure is applied enough times.

You will be given the value of n. Determine how many times this procedure must be applied before n becomes 1.


The initial value of n.


The number of operations we have to perform on n before it becomes 1.

Sample Input


Sample Output



n will go through these steps:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1


Any value of n, initial or intermediate, will be less than 231.

All Submissions
Best Solutions

Point Value: 5
Time Limit: 2.00s
Memory Limit: 16M
Added: Feb 19, 2010

Languages Allowed:

Comments (Search)

thank you asdflkjh will be proud :)

I had done many other questions and this was no exception.
I used Java and copied the code onto the project name.
However, it still gives me a compile error saying that "public class 3nplus1{" is incorrect.

3nplus1 is an exception. Java does not permit you to begin class names with numbers. This conflicts with our requirement to use the problem code as the class name.

We're working on a way to resolve this, but for now, this problem cannot be done in Java.

Change the problem code to something like threenplusone?

Changing problem codes is not trivial, but we do plan to allow it eventually.

I would like to know when you have resolved this error for Java


Maybe you could take Java off the languages allowed if it doesn't compile properly?

Half a year later...
And the problem is still not fixed.

actually 4 years later

its fixed now u satisfi3d

It should be possible to solve it in Java now.

3nplus1.cpp:2:14: error: invalid suffix "nplus1" on integer constant
3nplus1.cpp:1: error: ‘import’ does not name a type
3nplus1.cpp:2: error: expected unqualified-id before ‘public’
can you please help me? i really want to pass this problem!

This problem is not currently accepting Java submissions. Submitting your Java program as a C++03 program is not going to work.

Unhandled Exception: System.Security.Cryptography.CryptographicException: Couldn't access random source.
at System.Security.Cryptography.RNGCryptoServiceProvider.Check () [0x00000]
at System.Security.Cryptography.RNGCryptoServiceProvider..ctor () [0x00000]
at System.Guid.FastNewGuidArray () [0x00000]
at System.Reflection.Emit.ModuleBuilder..ctor (System.Reflection.Emit.AssemblyBuilder assb, System.String name, System.String fullyqname, Boolean emitSymbolInfo, Boolean transient) [0x00000]
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name, System.String fileName, Boolean emitSymbolInfo, Boolean transient) [0x00000]
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name, System.String fileName, Boolean emitSymbolInfo) [0x00000]
at Mono.CSharp.CodeGen.Init (System.String name, System.String output, Boolean want_debugging_support) [0x00000]
at Mono.CSharp.Driver.Compile () [0x00000]
at Mono.CSharp.Driver.Main (System.String[] args) [0x00000]

In the language dropdown list, it clearly says that C# is "(NOT WORKING)".

This will be fixed eventually, but it's very low-pri.