Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge

Name: Anonymous 2007-09-04 15:06 ID:bG0CVxDp

Hey, I've stolen this challenge from some website, it's pretty easy but I know most of you will fuck it up.

A sequence is defined by:

n -> n/2 (n is even)
n -> 3n + 1 (n is odd)

Find the longest sequence, with a starting number under one million.

Bonus points when you find the solution for ten million.

Name: Anonymous 2007-09-05 12:15 ID:lmR0Mh+D


        int maxPosition = 0;
        int maxLength = 0;
       
        for (int i = 1; i < max; i++) {
            int cnt;
            long value;
            for (cnt = 1, value = i; value != 1; ++cnt) {
                if (max - value > 0 && skip[(int) value] != 0) {
                    cnt += skip[(int) value];
                    break;
                }
                value = 0 == (value & 1) ? value >> 1 : value + (value << 1)
                        + 1;
            }
            if (cnt > maxLength) {
                maxLength = cnt;
                maxPosition = i;
            }
            skip[i] = cnt;
        }
        System.out.println("Nr: " + maxPosition);

time java XBOXITERATION
Nr: 837799

real    0m0.432s
user    0m0.360s
sys     0m0.036s

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List