vendredi 9 juin 2017

Error while attempting to solve project euler assignment

I'm working on solving the 14th project euler assignment.

This is the assignment:

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? NOTE: Once the chain starts the terms are allowed to go above one million.

This is my code:

package collatzSequence;

public class CollatzSeq {

    public static void main(String[] args) {
        int count = 0;
        int largestCount = 0;

        for (int i = 13; i < 1000000; i++) {
            // System.out.println(i);
            int Number = i;

            while (Number > 1) {
                count = 0;

                if (Number % 2 == 0) {
                    Number = i / 2;
                    System.out.println("Even: " + Number);
                } else {
                    Number = (Number * 3) + 1;
                    System.out.println("Uneven: " + Number);
                }
                count+=1;

                if (count > largestCount) {
                    largestCount = count;
                    System.out.println("New largest found");
                }

            }

        }

    }

}

Now, here's my problem. Everytime I run the program, it prints "Even: 6" over and over again. And its supposed to divide it by half if its even.

Does anyone recognize any problem with my code?

Aucun commentaire:

Enregistrer un commentaire