3x+1 - Collatz Conjecture

I made a new Python program that calculates numbers for the Collatz Conjecture.
Essentially it takes a number and if the number is odd you multiply it by 3 and add, 1 hence the name 3x+1, if it is even you divide the number by 2. Once it reaches 1 it gets in a loop (4,2,1,4,2,1), the numbers are considered to end when the reach one. Any time it is odd and you calculate 3x+1, it becomes an odd number. Some odd numbers divided by 2 become even others stay odd.

The conjecture became very famous due to the inability to prove that every number reaches one. Some numbers climb very high, for example 27 it’s highest number is 9232.

Wikipedia

Paul Erdős said about the Collatz conjecture: “Mathematics may not be ready for such problems.”[7] Jeffrey Lagarias stated in 2010 that the Collatz conjecture “is an extraordinarily difficult problem, completely out of reach of present day mathematics”.[8]

Collatz Conjecture Calculator

I hope you all enjoyed this showcase. If you have any questions I would be happy to answer them.

I have been really enjoying making small Repl’s for math concepts

2 Likes

Hey I made one too! Very cool!
https://replit.com/@CoderElijah/3x1

2 Likes

Same here a couple years ago: replit.com/@python660/3x1-test-case (March 17 2022)

Very nice, and the challenge is to optimize it so that it runs as fast as possible.

1 Like

There are several improvements to be made:

  1. When you apply the 3x+1 to an odd number, it is guaranteed to become even. So divide immediately.
  2. Accept only integers. Displaying them as floats looks bad, loses precision over time, and I’m pretty sure it’s only supposed to be ints.
  3. There are more loops for negative numbers (there are 3 negative loops discovered!)
1 Like

They will always be, I just have that so it can process numbers like this 1e10 or 2e5.

Correct, I could expand on this further, but I started with the positive numbers.

2 Likes

Ah, well then just convert to int at the end maybe.

1 Like

Great idea, I don’t know why I didn’t think of that.

Basically all negative ints will get the program into an infinite loop.

Also if anyone has any math related things I could try next I would be happy to except your ideas.

I would have to fix that soon to have support for negative numbers.

A more complicated math thing is “percolation”, which requires graphics.
-https://www.youtube.com/watch?v=a-767WnbaCQ

That is very interesting, I’ll look into trying to do it, thanks for the idea!

Added all three known negative loops and zero loop, ending in -5, ending in -1, ending in -17, and 0.

well in your calculator if you reach a number you’ve reached before immediately drop it. It takes care of loops and duplicate ccalculations

There’s only a few loops discovered, 3 for negative numbers, 1 for positive.
Storing all of the sequences of numbers probably takes a lot of memory and the program currently only does one calculation (it should allow more in one session though).

1 Like

for me i just go through all numbers sequencially and if the new number is less than the original number then stop.

What exactly does this mean, how would I store the sequences, do you mean the loops and I check if the numbers sequence matches?

equation solver (linear, quadratic, expressions, etc) im creating one rn
already have a ax+by=c solver

It’s funny that making a program like this is extremely easy
But optimise it like you guys did, is hard

How do I even understand everything you guys did, and still can’t write the same thing when asked to do so

1 Like

That sounds fun, I did one for one of the free code camp things, I could remake it though.