The Collatz Conjecture is a conjecture that a certain algorithm always terminates. The algorithm is stated as follows: starting with some positive integer n, divide n by two if it is even and otherwise triple n and add one. The algorithm terminates when n reaches one. It is currently an open problem whether this terminates for all positive integers. It is also called the Hailstone Sequence.
Questions tagged [collatz]
284 questions
-1
votes
2 answers
How do I make it so it can loop?
I need to test whether n is a multiple of 2 and then divide the number by 2. If the number isn't a multiple of 2, then I have to do 3*n+2.
How do I make it loop so I can get the following: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1?
Here's my current…

Marcelo Zevallos
- 11
- 1
-1
votes
1 answer
max collatz sequence python 3
I am trying to solve a problem where recursion is a must. The tasks is: Write a function that takes in an integer n and returns the highest integer in the corresponding Collatz sequence.
My solution is this:
collatz = []
def max_collatz(num):
…

mustafa bodur
- 3
- 1
-1
votes
2 answers
Recursion: Collatz sequence -Could someone please explain how this code returns the total number of steps?
#include
int collatz(int n);
int main(void)
{
int n = get_int("Enter int: ");
int steps = collatz(n);
printf("%i\n", steps);
}
int collatz(int n)
{
if (n==1)
{
return 0;
}
else if ((n%2)==0)
{
…

Dan
- 11
- 3
-1
votes
1 answer
checking if a very large number is divisible by 2 comes wrong
I am trying some stuff on the 3x+1 problem and noticed when I check if this num is divisible by two comes always true even that the second iteration should be false it goes like this for 20 iterations and then start to act right. I guess it is a…

Bros
- 1
- 2
-1
votes
1 answer
Writing a function to perform Collatz Conjecture
I am asked to complete the rest of this code:
def collatz_step(n):
if n % 2 == 0:
return n // 2
and write a while loop that will set n to the next value given by collatz_step until it reaches 1, printing the value of n at each…

Bath
- 17
- 4
-1
votes
3 answers
Collatz conjecture in JavaScript
everyone!
I'm trying to solve this for school. I need to count how many steps it takes to come from any positive integer down to 1. And I'm supposed to use TypeScript.
The Collatz Conjecture or 3x+1 problem can be summarized as follows:
Take any…

Mikhail
- 49
- 2
- 10
-1
votes
1 answer
Is there a way to go to specific line or command in python?
I am going through book automate boring stuff with python and trying to solve the practical problems.
In this project I need to define collatz() function and print results as seen in code until it gets 1.
So basically i need to input only one number…

spiro10
- 19
- 4
-1
votes
2 answers
How would I write an algorithm to find the greatest jump from number to number in a sequence?
If your sequence is 4 2 1, the largest jump is from 4 to 2. If your sequence is 3 10 5 16 8 4 2 1, the largest jump is from 5 to 16.
I've made an algorithm however I'm not completely sure what I have done wrong (whever I haven't made the loop…

nrbit
- 9
- 1
-1
votes
3 answers
Why is the code throwing segmentation fault?
The problem is to find the number i<=n, n<=500000 for which the longest collatz series exists.
Collatz series for a number n terminates at 1, and the conditions are
if n is even, next term = n/2
if n is odd, next term = 3*n + 1
Well as a matter of…

Sukrit Kapil
- 103
- 6
-1
votes
3 answers
Can't figure out how to increment a variable within a recursive function
I am trying to create a python script for the collatz conjecture (if a number's odd, times it by three and add 1, and if it's even, divide by two). I can't figure out how to successfully increment a variable that shows how many iterations of the…

liam6666
- 19
- 1
- 9
-1
votes
1 answer
How to fix my Collatz sequence code that exceeds the 1-minute rule?
Summary
Hello everyone. I am trying to solve a -relatively easy- problem, related to the Collatz Conjecture.
First and foremost, the problem reads as follows:
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n…

ripeat
- 9
- 5
-1
votes
2 answers
The 3n+1 algorithm
Some integer n is given. If n is odd, carry out a task of 3 n+1 and n / 2 if n is even. Finish work when n reaches 1. The number created before n reaches 1 is called the cycle length. When two numbers, I and j, try to get the maximum cycle length…

baek
- 1
- 1
-1
votes
5 answers
How can I solve the Collatz conjecture algorithm in C#?
I was able to solve the Collatz conjecture algorithm (no, i didn't try to prove it) in about 5 minutes using Java.
Now that I'm learning C# to make web apps, I'm running into trouble doing the same thing.
I simply want the user to enter a number,…

tmp
- 11
- 1
- 2
-1
votes
2 answers
Collatz sequence using linq c#
Good afternoon guys, I have a following problem of the school to solve, being it in relation to Collatz Problem, where I need to develop an application that will find out the starting number between 1 and 1 million that produces the largest…
-1
votes
1 answer
Dr. Racket: saving intermidiate results - Collatz Conjecture
I am new into coding and got interested in Dr. Racket and I am facing my first problem now. I created this code:
(define (collatz n)
(cond ((= n 1) 1)
((> n 1) (collatz_helper n))))
(define (collatz_helper n)
(if (even? n)
…

herbrand
- 13
- 3