Schools out now, and for my final grade my programming teacher gave us the option to come up with our own project to research about, problem solve, code, and present to the class. My project dealt with the Collatz conjecture just because some friends of mine who i visited last year during the AOTM summer camp told me about it, and got me extremely interested in how it works.

For people who don't know what the Collatz conjecture is; basically all it states is that if you have a even number plug that number into this equation: n/2, and if you have an odd number use this equation: 3n+1. The Conjecture states that no matter what number you put in, you will eventually end up at the number 1.

For example, the number 3:

3 is odd so we use 3n+1

3(3)+1 = 10

10 is even so we use n/2

10/2 = 5 3(5)+ 1 = 16 16/2 = 8 8/2 = 4 4/2 = 2 2/2 = 1

More information here

Given these standards, we can see that each number has a given number of "steps", or number of times you have to use the conjecture to reach the number 1. So like for the number 3 we got 3, 10, 5, 16, 8, 4, 2 , and 1 which is 8 steps(7 if 3 is not included). Here is where my final project comes into play. I asked myself the question "If given the number of steps, can we find the number or numbers that have that amount of steps? In other words, if i was to say "what number has 8 steps?" how could i find that number, and whether or not there are multiple numbers that have that many steps.

So basically my question is can we find all real numbers that derive from the steps of any real number using the Collatz conjecture.

To do this a friend of mine and I came together and wrote this code in java that basically inputs a number starting from 1 into the Collatz conjecture. Then it counts the number of steps it takes for that number, then checks to see if the number of steps matches the number inputted by the user. If the number of steps does not match the number asked then it tries the next number and so on.

The written code can be found here

Class here

This code works and all and i got a 100 on my final grade for it, but it doesn't answer the question of finding all integers. Instead, the program just finds the first number and sends it to the user.

TL;DR How can I update my code so that it lists ALL of the numbers that have the given number of steps?

## Comments

Sort by:

TopNewestWrote this up in python real quick but not super experience with code so it's not necessarily super efficient:

Takes initial array "arr" of values from last collatz cycle, at the beginning this is just 1. Then it stores another array of potential values for the next level of the collatz cycle. Finally there is a store of previous numbers so you don't repeat old numbers that say, had a collatz number of 4 showing up in level 7. In the for loop, it checks to see, for each number in the current level, what numbers could get the number from the next level up and checks to see that they are each valid. Then it adds the array to the previous numbers and makes the current array the next array and just loops for the number of steps you enter.

For example entering 8 at the prompt returns [3, 20, 21, 128] – Sean Sullivan · 1 year, 9 months ago

Log in to reply

– Nathan Antwi · 1 year, 9 months ago

Holy crap. This is very nice. Props and #Respect. Good job hahaha.Log in to reply

Congratulations on getting a 100.

The Collatz conjecture is for integers, not real numbers – Agnishom Chattopadhyay · 1 year, 9 months ago

Log in to reply

– Prasun Biswas · 1 year, 9 months ago

Positive integer, to be precise.Log in to reply

– Nathan Antwi · 1 year, 9 months ago

Ahhh my mistake.. ThanksLog in to reply

You can even try this out for fun. – Kishlaya Jaiswal · 1 year, 9 months ago

Log in to reply