# Collatz Conjecture

The Collatz Conjecture (which was seen in the If Challenge), goes like this:

Take any number n. If n is even, divide it by 2, if n is odd, multiply it by 3 and add 1. Repeat the process indefinitely, and you'll eventually reach 1.

A Collatz sequence is the sequence of number generated from a specific `number.`

(From XKCD.)

Input/Output Format
Boilerplate code is provided for Java. For other languages, read in the numbers from the standard input in the following format:

• The input starts with N, the number of cases.
• N lines follow, which each contains 1 integer.

Print out the correct numbers for each case on its own line.

Click on "View Raw I/O" below to view sample input and output.

### Challenge

Given a `num` variable, can you print all the numbers in `num`'s Collatz sequence until 1 is reached? Print the numbers space-separated (and each test case on its own line).

Alternatively, you can try out Learneroo before signing up.

• #### zkvarz

Quite simple, one problem was to stop the toop, eventually I've managed it.

• #### David

For me it wasn't about stopping the loop but actually how to get the number print the first time(initial input) and the last time (1) without having the loop interfering.

• #### Mikas

Whats wrong with my code? It keeps giving me the "Runtime error"

``````public static void doStuff(int num){
System.out.print(num + " ");
while(num > 1){
``````
• #### Melany

@Mikas Your else says "num = num * 3 - 1" it should be +1.

• #### Jig

I dont understand why would 3-1 cause runtime error?

• #### Learneroo

@Jig, you can get stuck in a loop, eg 5, 14, 7, 20, 10, 5 ...

• #### Jig

How did you know that it would get stuck in a loop. Thanks for the insight. Love what you guys are doing.

• #### Bernard Mitchell

@Learneroo should I create two loops?

• #### Learneroo

@Bernard, that's not necessary. You can create one `while` loop, with a condition inside it that determines what number to go to next.