Mohannad Webman
Mohannad Webman

Reputation: 19

Collatz conjecture: Count how many have the length 111

So my question is about the collatz conjecture. The task is that I have to write a code which counts the length of steps of each conjecture. For example 2 = 2/2 = 1 is one step.

Now I need to know how many start numbers between 1 and 10000 have 111+ steps. I get 54 on my code but I don't have any sources wether its right or wrong.

anzahl should be the counter of how many numbers have 111 steps and laenge is the length of each number:

public class Collertz {

    public static void main(String[] args) {
        int max = 111;
        int anzahl = 0;
        int laenge = 0;

        for(int i=0; i<=10000;i++) {
            for (int j = i; i>1; i++) {
                if (j%2 == 0) {
                    j = j/2;
                    laenge++;
                }
                else {
                    j = 3*j+1;
                    laenge++;
                }
            }
            if (laenge >= max) {
                anzahl++;
            }
        }
        System.out.println(anzahl);
    }
}

Upvotes: 1

Views: 730

Answers (3)

user unknown
user unknown

Reputation: 36269

Here are the issues resolved, as mentioned in my comment:

static int collatz (int max) {
    int anzahl = 0;
    for(int i=1; i<=10000; ++i) {
        for (int j = i, laenge=0; j>1; laenge++) {
            if (j % 2 == 0) 
                j /= 2;
            else 
                j = 3*j+1;
            if (laenge == max) {
                anzahl++;
                break;
            }
        }
    }
    System.out.println(anzahl);
    return anzahl;
}
collatz (111);
  • laenge++ is performed in every branch of the if statement.
  • laenge is reset for every new number.
  • we break the inner for-loop when reaching anzahl.
  • i++ in the inner loop was wrong.
  • it's j which needs to stay above 1 to proceed, not i

I didn't look up, what the rules for the collatz-condingsbums are, it sounded reasonable. :)

Upvotes: 0

Ingo
Ingo

Reputation: 36339

Your code suffers from the bad habit to write everything in the main method.

You should at least write a (pure) function that gives you the length of the sequence for some argument. Then a simple for loop is enough to find out how many function values fit your criteria.

But since you prefer to write everything in one go, you promptly introduced a severe error. Just think about what the variable laenge gives you? What is its intended purpose? And does it fulfil that purpose or should it rather read sumOfLengths.

Upvotes: 0

Steve Brandli
Steve Brandli

Reputation: 566

I see two problems with your code, one major and one minor. The major problem is that you test and increment i in the for statement of your second loop. Should you not be testing j and not incrementing anything? I would have used a while loop myself. The small problem is that your first loop starts with 0 rather than 1. But this should not impact your results.

Upvotes: 0

Related Questions