Comment detail

コラッツ・角谷の問題 (Nested Flatten)

Pen4 2.40GHz で、以下のような結果になりました。最初にIntegerでやっていたので、113383 ではまってました。

f(837799)=524 elapse: 15469(ms)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Sample120 {
    private final long maxIndex_;
    private final Map<Long, Long> memo = new HashMap<Long, Long>();

    public Sample120(int max) {
        long maxInd = 0;
        long maxLen = 0;
        List<Long> sequence = new ArrayList<Long>();
        for (long index = 1; index <= max; index++) {
            sequence.clear();
            long len = calc3n1(index, 0, sequence);
            createMemo(len, sequence);
            if (maxLen < len) {
                maxLen = len;
                maxInd = index;
            }
        }
        maxIndex_ = maxInd;
    }
    private long calc3n1(long x, long length, List<Long> sequence) {
        sequence.add(x);
        if (x == 1) return length;

        Long longVal = memo.get(x);
        if (longVal != null) return length + longVal.longValue();

        if ((x & 1) == 0) {
            return calc3n1(x / 2, length + 1, sequence);
        } else {
            return calc3n1(3 * x + 1, length + 1, sequence);
        }
    }
    private void createMemo(long length, List<Long> sequence) {
        long len = length;
        for (int index = 0, max = sequence.size(); index < max; index++) {
            Long longVal = sequence.get(index);
            if (memo.get(longVal) == null) {
                memo.put(longVal, len--);
            }
        }
    }

    public long getMaxIndex() {
        return maxIndex_;
    }
    public long getMaxLength() {
        return memo.get(maxIndex_);
    }


    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        Sample120 instance = new Sample120((int)Math.pow(2, 20));
        System.out.println(instance.getMaxIndex() + ":" + instance.getMaxLength());

        long end = System.currentTimeMillis();
        System.out.println("elapse: " + (end - start) + "(ms)");
    }
}

Index

Feed

Other

Link

Pathtraq

loading...