Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Post History

#10: Post edited by user avatar trichoplax‭ · 2023-07-06T13:31:19Z (10 months ago)
Add finalized tag now that the sandbox can be filtered to exclude tags
Collatz conjecture; Count the tries to reach $1$ [released]
# Background

Check out [this video on the Collatz conjecture](https://www.youtube.com/watch?v=094y1Z2wpJg), also known as A006577[^1].

If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:

- If $x$ is odd, then $3x + 1$.
- If $x$ is even, then $\frac{x}{2}$.

This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.

# Challenge

Write a program that establishes the Collatz conjecture:

- Take input of a positive integer. This will be the $x$ of the problem.
- Read the background for how it works, or watch the video for further explanation.
- The result should be how many turns it would take before reaching $1$. There, the sequence stops.
- This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!

# Test Cases

From 1 to 10:
```
1  → 0  (1)
2  → 1  (2 → 1)
3  → 7  (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
4  → 2  (4 → 2 → 1)
5  → 5  (5 → 16 → 8 → 4 → 2 → 1)
6  → 8  (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
7  → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
8  → 3  (8 → 4 → 2 → 1)
9  → 19 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
10 → 6  (10 → 5 → 16 → 8 → 4 → 2 → 1)
```

More of these can be found on OEIS (see reference 1). Thanks to [**@Shaggy**](https://codegolf.codidact.com/users/53588) for the link!

[^1]: https://oeis.org/A006577
#9: Post edited by user avatar General Sebast1an‭ · 2021-09-23T13:19:57Z (over 2 years ago)
  • Collatz conjecture; Count the tries to reach $1$
  • Collatz conjecture; Count the tries to reach $1$ [released]
#8: Post edited by user avatar General Sebast1an‭ · 2021-09-16T10:43:34Z (over 2 years ago)
  • Collatz conjecture; Count the tries to reach $1$ [in test (beta)]
  • Collatz conjecture; Count the tries to reach $1$
#7: Post edited by user avatar General Sebast1an‭ · 2021-09-14T11:40:28Z (over 2 years ago)
  • Collatz conjecture; Count the tries to reach $1$
  • Collatz conjecture; Count the tries to reach $1$ [in test (beta)]
#6: Post edited by user avatar General Sebast1an‭ · 2021-09-08T09:03:42Z (over 2 years ago)
  • # Background
  • Check out [this video on the Collatz conjecture](https://www.youtube.com/watch?v=094y1Z2wpJg).
  • If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the Collatz conjecture:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 5 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 19 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 6 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
  • # Background
  • Check out [this video on the Collatz conjecture](https://www.youtube.com/watch?v=094y1Z2wpJg), also known as A006577[^1].
  • If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the Collatz conjecture:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 5 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 19 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 6 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
  • More of these can be found on OEIS (see reference 1). Thanks to [**@Shaggy**](https://codegolf.codidact.com/users/53588) for the link!
  • [^1]: https://oeis.org/A006577
#5: Post edited by user avatar General Sebast1an‭ · 2021-09-08T07:07:24Z (over 2 years ago)
  • # Background
  • Check out [this video on $3x + 1$](https://www.youtube.com/watch?v=094y1Z2wpJg).
  • If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the $3x + 1$ problem:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 5 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 19 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 6 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
  • # Background
  • Check out [this video on the Collatz conjecture](https://www.youtube.com/watch?v=094y1Z2wpJg).
  • If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the Collatz conjecture:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 5 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 19 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 6 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
#4: Post edited by user avatar General Sebast1an‭ · 2021-09-07T11:12:59Z (over 2 years ago)
  • # Background
  • Check out [this video on $3x + 1$](https://www.youtube.com/watch?v=094y1Z2wpJg).
  • If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the $3x + 1$ problem:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 6 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 18 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 7 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
  • # Background
  • Check out [this video on $3x + 1$](https://www.youtube.com/watch?v=094y1Z2wpJg).
  • If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the $3x + 1$ problem:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 5 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 19 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 6 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
#3: Post edited by user avatar General Sebast1an‭ · 2021-09-07T09:05:16Z (over 2 years ago)
  • # Background
  • Check out [this video on $3x + 1$](https://www.youtube.com/watch?v=094y1Z2wpJg).
  • If you know what this is, we're given an equation of $3x + 1$, and it is applied this way in the video:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the $3x + 1$ problem:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 6 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 18 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 7 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
  • # Background
  • Check out [this video on $3x + 1$](https://www.youtube.com/watch?v=094y1Z2wpJg).
  • If you don't know what this is, we're given an equation of $3x + 1$, and it is applied this way:
  • - If $x$ is odd, then $3x + 1$.
  • - If $x$ is even, then $\frac{x}{2}$.
  • This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.
  • # Challenge
  • Write a program that establishes the $3x + 1$ problem:
  • - Take input of a positive integer. This will be the $x$ of the problem.
  • - Read the background for how it works, or watch the video for further explanation.
  • - The result should be how many turns it would take before reaching $1$. There, the sequence stops.
  • - This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!
  • # Test Cases
  • From 1 to 10:
  • ```
  • 1 → 0 (1)
  • 2 → 1 (2 → 1)
  • 3 → 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 4 → 2 (4 → 2 → 1)
  • 5 → 6 (5 → 16 → 8 → 4 → 2 → 1)
  • 6 → 8 (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 7 → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 8 → 3 (8 → 4 → 2 → 1)
  • 9 → 18 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
  • 10 → 7 (10 → 5 → 16 → 8 → 4 → 2 → 1)
  • ```
#2: Post edited by user avatar General Sebast1an‭ · 2021-09-07T09:04:51Z (over 2 years ago)
  • $3x + 1$; Count the tries to reach $1$
  • Collatz conjecture; Count the tries to reach $1$
#1: Initial revision by user avatar General Sebast1an‭ · 2021-09-07T08:50:52Z (over 2 years ago)
$3x + 1$; Count the tries to reach $1$
# Background

Check out [this video on $3x + 1$](https://www.youtube.com/watch?v=094y1Z2wpJg).

If you know what this is, we're given an equation of $3x + 1$, and it is applied this way in the video:

- If $x$ is odd, then $3x + 1$.
- If $x$ is even, then $\frac{x}{2}$.

This will send us in a loop of `4 → 2 → 1 → 4 → 2 → 1...`, which got me into making this challenge.

# Challenge

Write a program that establishes the $3x + 1$ problem:

- Take input of a positive integer. This will be the $x$ of the problem.
- Read the background for how it works, or watch the video for further explanation.
- The result should be how many turns it would take before reaching $1$. There, the sequence stops.
- This is <a class="badge is-tag">code-golf</a>, so the shortest program in each language wins!

# Test Cases

From 1 to 10:
```
1  → 0  (1)
2  → 1  (2 → 1)
3  → 7  (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
4  → 2  (4 → 2 → 1)
5  → 6  (5 → 16 → 8 → 4 → 2 → 1)
6  → 8  (6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
7  → 16 (7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
8  → 3  (8 → 4 → 2 → 1)
9  → 18 (9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1)
10 → 7  (10 → 5 → 16 → 8 → 4 → 2 → 1)
```