Problem 14. Longest Collatz sequence

import java.util.*;

public class euler14 {
    public static void main(String[]args) {
        int max = 0;
        int i = 0;
        int num = 0;
        HashSet s = new HashSet();
       	final int MAX = 50000000;
        int [] array = new int[MAX];
        for (i = 3; i < 1000000; i+=2) {
            if (array[i] == 0) {
                long n = i;
              	int count = 0;
               	while (n != 1) {
                    if (n % 2 == 0) {
                        n /= 2;
                    }
                    else
                        n = 3*n + 1;
                    if (n < MAX) {
                        int k = (int)n;
                        array[k] = 1;
                    }
                    count++;
                }
                if (max < count) {
                    max = count;
                    num = i;
                }
            }
        }
        System.out.println(num);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>