The **Collatz Conjecture**, also known as ** 3n + 1** conjecture,

**Ulam Conjecture**,

**Thwaites conjecture**,

**Hasseâ€™s algorithm**,

**Kakutaniâ€™s problem**, or the

**Syracuse problem**, is a conclusion formed on the basis of incomplete information in mathematics,

The **Collatz Conjecture** is an eventually one of the unsolved problem in mathematics and applying the following algorithm to any number we will always eventually reach **one**. It can be summarized as follows:

- Start with any positive integer.
- Each new term is obtained from the previous term:
- If the previous term is
**even**, the next term is**half**of the**previous**term i.e ().`previous / 2`

- If the previous term is
**odd**, the next term is**3 times the previous term plus 1**i.e ().`3 * previous + 1`

- If the previous term is
- Repeat the process indefinitely. No matter what value of n is, the sequence will always reach 1.

### Algorithm

1Step 1: Start2Step 2: Declare variables n.3Step 3: Read n from the user.4Step 4: Repeat the steps until n > 15 4.1 If n % 2 === 06 n = n / 27 Else8 n = 3 * n + 19Step 5: Stop

Given a number n, return the number of steps required to reach 1.

### Examples

1function getCollatzConjectureSequence(value) {2 if (value < 1) {3 throw new Error('Only positive numbers are allowed')4 }5 let count = 067 console.log("Sequence starts");8 console.log(value);910 while (value > 1) {11 value = value % 2 === 0 ?12 value / 2 :13 (3 * value) + 1;1415 console.log(value);1617 count++18 }1920 console.log("Sequence ends");21 console.log("Count", count);22}2324getCollatzConjectureSequence(12)

Starting with n = 12, results in 9 steps

1Sequence starts212364351065716889410211112Sequence ends13Count 9

Hope you learn something new. If this article was helpful, share it.

Happy coding