Collat​​z序列:使用Java进行动态编程

时间:2017-10-24 21:40:37

标签: java

我正在尝试使用memoization技术实现Collat​​z序列,但是我对最初的输出感到困惑,n_ {0} = 13.

预期产量: [13,40,20,10,5,16,8,4,2,1]

实际输出: [1,2,4,8,16,5,10,20,40,13,1,0]

注意,奇怪的顺序(不应该是自上而下的记忆)。另外,我最后得到了一些额外的元素[1,0]。我想我需要在n == 1?

处终止递归调用

非常感谢。

 public static void collatzGeneratorCall(int n){
        //collatzGenerator(n,  new ArrayList<Integer>(Collections.nCopies(1, 0)));
        collatzGenerator(n,  new ArrayList<Integer>(Collections.nCopies(1, 0)));


    }

    private static void collatzGenerator(int n,List<Integer>arrayList) {

        int array_vals = arrayList.get(sequenceInts);

        if (array_vals == 0) {

            if (n == 1) {
                arrayList.add(sequenceInts, n);
                //n = -1;
                //sequenceInts += 1;

            }
            if (n % 2 == 0) {
                collatzGenerator(n / 2, arrayList);
                arrayList.add(sequenceInts,n);
                sequenceInts += 1;


            } else if (n % 2 != 0) {
                collatzGenerator((3 * n) + 1, arrayList);
                arrayList.add(sequenceInts,n);
                sequenceInts += 1;


            }


        }

        System.out.println(arrayList); }

1 个答案:

答案 0 :(得分:0)

我冒昧地为你建立一个可行的榜样:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class CollatzGenerator {
  private transient Map<Integer, CollatzNode> memoizationMap;

  public Optional<CollatzNode> getMemoizedDecomposition(final int input) {
    if (memoizationMap == null) {
      memoizationMap = new HashMap<>();
      memoizationMap.put(1, new BaseNode());
    }
    return Optional.ofNullable(memoizationMap.get(input));
  }

  public IntStream getCollatzDecomposition(final int input) {
    return computeCollatzNode(input).toStream().mapToInt(i -> i);
  }

  private CollatzNode computeCollatzNode(final int input) {
    final Optional<CollatzNode> memoizedResult = getMemoizedDecomposition(input);
    if (memoizedResult.isPresent()) {
      return memoizedResult.get();
    } else if (input % 2 == 0) {
      return new ComputedNode(input, computeCollatzNode(input / 2));
    } else {
      return new ComputedNode(input, computeCollatzNode(input * 3 + 1));
    }
  }

  private interface CollatzNode {
    Stream<Integer> toStream();
  }

  private class BaseNode implements CollatzNode {

    @Override
    public Stream<Integer> toStream() {
      return Stream.of(1);
    }
  }

  private static class ComputedNode implements CollatzNode {
    private final int value;
    private final CollatzNode nextValue;

    private ComputedNode(final int value, final CollatzNode nextValue) {
      this.value = value;
      this.nextValue = nextValue;
    }

    @Override
    public Stream<Integer> toStream() {
      return Stream.concat(Stream.of(value), nextValue.toStream());
    }
  }

  public static void main(String[] args) {
    final CollatzGenerator collatzGenerator = new CollatzGenerator();
    System.out.println(Arrays.toString(collatzGenerator.getCollatzDecomposition(13).toArray()));
    System.out.println(Arrays.toString(collatzGenerator.getCollatzDecomposition(27).toArray()));
    System.out.println(Arrays.toString(collatzGenerator.getCollatzDecomposition(6171).toArray()));
    System.out.println(Arrays.toString(collatzGenerator.getCollatzDecomposition(97).toArray()));
    System.out.println(Arrays.toString(collatzGenerator.getCollatzDecomposition(871).toArray()));
  }
}

这会产生:

[13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
[27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
[6171, 18514, 9257, 27772, 13886, 6943, 20830, 10415, 31246, 15623, 46870, 23435, 70306, 35153, 105460, 52730, 26365, 79096, 39548, 19774, 9887, 29662, 14831, 44494, 22247, 66742, 33371, 100114, 50057, 150172, 75086, 37543, 112630, 56315, 168946, 84473, 253420, 126710, 63355, 190066, 95033, 285100, 142550, 71275, 213826, 106913, 320740, 160370, 80185, 240556, 120278, 60139, 180418, 90209, 270628, 135314, 67657, 202972, 101486, 50743, 152230, 76115, 228346, 114173, 342520, 171260, 85630, 42815, 128446, 64223, 192670, 96335, 289006, 144503, 433510, 216755, 650266, 325133, 975400, 487700, 243850, 121925, 365776, 182888, 91444, 45722, 22861, 68584, 34292, 17146, 8573, 25720, 12860, 6430, 3215, 9646, 4823, 14470, 7235, 21706, 10853, 32560, 16280, 8140, 4070, 2035, 6106, 3053, 9160, 4580, 2290, 1145, 3436, 1718, 859, 2578, 1289, 3868, 1934, 967, 2902, 1451, 4354, 2177, 6532, 3266, 1633, 4900, 2450, 1225, 3676, 1838, 919, 2758, 1379, 4138, 2069, 6208, 3104, 1552, 776, 388, 194, 97, 292, 146, 73, 220, 110, 55, 166, 83, 250, 125, 376, 188, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
[97, 292, 146, 73, 220, 110, 55, 166, 83, 250, 125, 376, 188, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
[871, 2614, 1307, 3922, 1961, 5884, 2942, 1471, 4414, 2207, 6622, 3311, 9934, 4967, 14902, 7451, 22354, 11177, 33532, 16766, 8383, 25150, 12575, 37726, 18863, 56590, 28295, 84886, 42443, 127330, 63665, 190996, 95498, 47749, 143248, 71624, 35812, 17906, 8953, 26860, 13430, 6715, 20146, 10073, 30220, 15110, 7555, 22666, 11333, 34000, 17000, 8500, 4250, 2125, 6376, 3188, 1594, 797, 2392, 1196, 598, 299, 898, 449, 1348, 674, 337, 1012, 506, 253, 760, 380, 190, 95, 286, 143, 430, 215, 646, 323, 970, 485, 1456, 728, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]