美文网首页
科拉茨猜想:3X+1问题

科拉茨猜想:3X+1问题

作者: 蓝笔头 | 来源:发表于2021-08-09 11:03 被阅读0次
package com.example.demo;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class DemoTest {

    public static void main(String[] args) throws Exception {
//        calcLast();
//        System.out.println("++++++++++++++++++++++++++++++++++++++++++");
//        calFirst();
        f(1001);

    }

    private static void f(int n) {
        print(n);
        if (n == 1 || n == 2 || n == 4) {
            return;
        }

        if ((n & 1) == 1) {
            f(n * 3 + 1);
        } else {
            f(n / 2);
        }
    }

    private static final Map<String, String> add = new HashMap<String, String>() {
        {
            put("100", "加1位");
            put("101", "加2位");
            put("110", "加2位");
            put("111", "加2位");
        }
    };

    private static final Map<String, String> sub = new HashMap<String, String>() {
        {
            put("001", "减2位");
            put("011", "减1位");
            put("101", "减4位");
            put("111", "减1位");
        }
    };

    private static void printAdd(int n ) {
        String s = Integer.toBinaryString(n);
        if (s.length() >= 3) {
            String prefix = s.substring(0, 3);
            System.out.printf(add.get(prefix) + " ----- ");
        }
    }

    private static void printSub(int n ) {
        String s = Integer.toBinaryString(n);
        if (s.length() >= 3) {
            String prefix = s.substring(s.length() - 3);
            System.out.printf(sub.get(prefix) + " ----- ");
        }
    }

    private static void calFirst() {
        for (int i = 0; i < 4; ++ i) {
            int a = (1 << 2) + i;

            calc(a);
            System.out.println();
            System.out.println("<===========================>");
            System.out.println();
        }
    }

    private static void calcLast() {
        for (int i = 0; i < 4; ++ i) {
            int a = (i << 1) + 1;
//            System.out.printf("%3s\n", Integer.toBinaryString(a));

            calc(a);
            System.out.println();
            System.out.println("<===========================>");
            System.out.println();
        }
    }

    private static void calc(int n) {
        int a1 = n;
        int a2 = n * 2;
        print(a1);
        print(a2);
        print(1);
        printLine();
        print(a1 + a2 + 1);
    }

    private static void print(int n) {
        if ((n & 1) == 1) {
            printAdd(n);
            printSub(n);
        }
        String s = Integer.toBinaryString(n);
        System.out.printf("%10d, %s\n", s.length(), s);
    }

    private static void printLine() {
        System.out.println("----------");
    }

}

输出:

...
加2位 ----- 减4位 -----         13, 1011111010101
        15, 100011110000000
        14, 10001111000000
        13, 1000111100000
        12, 100011110000
        11, 10001111000
        10, 1000111100
         9, 100011110
...

结尾有 7个 0,应该减 7 位。所以上述代码有误。

相关文章

网友评论

      本文标题:科拉茨猜想:3X+1问题

      本文链接:https://www.haomeiwen.com/subject/fthzvltx.html