3N+1猜想中判断正整数N是否能归一的Java程序验证

卡拉兹(Callatz)猜想、3N+1猜想的扩展阅读参考:https://tech.sina.com.cn/d/i/2019-03-18/doc-ihrfqzkc4740833.shtml

以下代码使用 StringBuilder 对象存储正整数N的二进制字符串序列,这样就可以验证大于Long.MAX_VALUE的数是否能归一。


    public static void main(String[] args) {
        //StringBuilder sb =new StringBuilder(Long.toBinaryString(27));
        StringBuilder sb = new StringBuilder("10000101010101010100011111000101101010101010000000100111100101010101111");
        check_3n1(sb);
    }
    
    /* 验证一个数经过卡拉兹变换能否归一。
     * StringBuilder natural : 二进制数字符串。
     * return true 能归一。
     */
    public static boolean check_3n1(StringBuilder natural) {
        int idx;
        char endchar, c1,c0,carry;

        // 删除串高位无效的0
        while(true) {
            endchar = natural.charAt(0);
            if (endchar == '0'){
                natural.deleteCharAt(0);
            } else {
                break;
            }
        }
        // 删除串低位的0,相当于是偶数时持续除2
        while(true) {
            endchar = natural.charAt(natural.length() - 1);
            if (endchar == '0'){
                natural.deleteCharAt(natural.length()-1);
            } else {
                break;
            }
        }

        while(true) {
            System.out.println(natural.toString());
            endchar = natural.charAt(natural.length() - 1);
            if (endchar == '1') {
                // (3n+1)/2 = ( (n<<1) + n+1) >> 1;
                natural.insert(0, "00"); // 预前补00来处理进位。
                natural.append('0'); //  左移一位(n+n = n<<1)
                idx = natural.length();
                carry = '0'; // 初始化时没有进位
                // +n
                for(int i=idx-1; i>0; i--) {
                    c1 = natural.charAt(i-1);
                    c0 = natural.charAt(i);
                    if (c1 == '0' && c0 == '0' && carry == '1') {
                        natural.replace(i, i+1, "1");
                        carry = '0';
                    } else if (c1 == '0' && c0 == '1' && carry == '1') {
                        natural.replace(i, i+1, "0");
                    } else if (c1 == '1' && c0 == '0' && carry == '0') {
                        natural.replace(i, i+1, "1");
                    } else if (c1 == '1' && c0 == '0' && carry == '1') {
                        natural.replace(i, i+1, "0");
                    } else if (c1 == '1' && c0 == '1' && carry == '0') {
                        natural.replace(i, i+1, "0");
                        carry = '1';
                    } else if (c1 == '1' && c0 == '1' && carry == '1') {
                        natural.replace(i, i+1, "1");
                    } else {
                        // (c1 == '0' && c0 == '0' && carry == '0')
                        // (c1 == '0' && c0 == '1' && carry == '0')
                    }
                }
                // +1
                carry = '1';
                for(int i=idx-1; i>0; i--) {
                    c0 = natural.charAt(i);
                    if (c0 == '0' && carry == '1') {
                        natural.replace(i, i+1, "1");
                        carry = '0';
                    } else if (c0 == '1' && carry == '1') {
                        natural.replace(i, i+1, "0");
                    } else {
                        // (c0 == '0' && carry == '0')
                        // (c0 == '1' && carry == '0')
                    }
                }
                // 是偶数时持续除2
                while(true) {
                    endchar = natural.charAt(natural.length() - 1);
                    if (endchar == '0'){
                        natural.deleteCharAt(natural.length()-1);
                    } else {
                        break;
                    }
                }

                if (natural.charAt(0) == '0'){
                    natural.deleteCharAt(0);
                }
                if (natural.charAt(0) == '0'){
                    natural.deleteCharAt(0);
                }
            } else {
                natural.deleteCharAt(natural.length()-1);
            }
            if (1 >= natural.length()) {
                break;
            }
        }
        System.out.println("true"); // 能跳出循环就能归一,否则...
        return true;
    }

运行结果:

10000101010101010100011111000101101010101010000000100111100101010101111
11000111111111111110101110101000011111111111000000111011011000000000111
100101011111111111110000101111100101111111110100001011001000100000001011
111000001111111111101001000111011000111111101110010000101100110000010001
101010001011111111101110110101100010101111110010101100100001100100001101
1111110100011111111001100100000101000001111011000000101100100101100101
10111101110101111110110010110000111100010111000100001000010111000011
100011100110000111110001100001001011010100010100110001100100010100101
110101011001001011101010010001110000111110011111001010010110011111
1010000000101110001011111011010101001011101101110101111100001101111
1111000001000101010001111000111111110001100100110000111010010100111
10110100001100111111010110101011111101010010111001001010111011111011
100001110010011011111000010000001111011111100010101110000011001111001
11001010101110100111010001100001011100111101010000010100010011011011
100110000000101111010111010010010001011011011111000011110011101001001
11100100000100011100001011101101101000100100111010010110110101110111
101010110000110101010010001100100011100110111010111100010010000110011
1000000001001001111111011010010110101011010011000011010011011001001101
11000000011011101111110001111000100000001111001001001111010001011101
1001000001010011001111010101101001100000101101011011101101110100011
1101100001111100110111000000011110010001000100001001100100101110101
1010001001011101101001010000010110101100110011000111001011100011
1111001110001100011101111000100010000011001100101010110001010101
1011011010101001010110011010011001100010011001100000000101
10001000111111110000001100111100110010011100110010000001
1100110101111110100001001101101100101110101100101100001
1001101000011110111000111010010001100011000001100001001
111001110010111001010101011101101001010010001001000111
1010110101100010110000000001100011101111011001101101011
10000010000010100001000000010010101100111000110100100001
1100001100001111000110000001110000001101010100111011001
1001001001001011010100100001010100001001111111101100011
1101101101110000111110110001111110001110111111100010101
101001001001010010111100010101111010101100111110101
11110110110111110001101010000011100000001101111
101110010010011101010011111000101010000010100111
1000101011011101011111101110100111111000011111011
1101000001001100001111100101111011110100101111001
1001110000111001001011101100011100110111100011011
1110101001010101110001100010101011010011010101001
1010111111000000010101001010000000011110011111111
10000011110100000011111101111000000101101101111111
11000101101110000101111100110100001000100100111111
100101000100101001000111011001110001100110111011111
110111100110111101101011000110101010011010011001111
1010011011010011100100000101001111111100111100110111
1111101000111101010110000111110111111011011011010011
10111011101011100000001001011110011111001001000111101
1000110011000010100000011100011011011101011011010111
1101001100100011110000101010101001001100001001000011
10011110010110101101000111111111101110010001101100101
11101101100010000011101011111111100101011010100011
101100100010011000101100001111111011000000111110101
100001011001110010100001001011111100010000101111
110010000110101011110001110001111010011001000111
1001011001010000001101010101010110111100101101011
1110000101111000010100000000000010011011000100001
1010100100011010001111000000000001110100010011001
111111011010011101011010000000001010111001110011
1011111000111101100000111000000010000010110101101
100011101010111000100010101000000110001000100001
11010110000001010011001111110000100100110011001
10100000100000111110011011110100011011100110011
11110000110001011101101001101110101001011001101
1011010010010100011000111101001011111100001101
100001110110111101001010110111100011110100101
110010110010011011110000010011010101101111
1001100001011101001101000011101000000100111
1110010010001011110011100101011100000111011
10101011011010001101101011000001010001011001
10000000100011101010010000010000111101000011
11000000110101011111011000011001011011100101
100100001010000001111000100100110001001011
110110001111000010110100110111001001110001
101000101011010010000111101001010111010101
111101000000111011001011011110000011
1011011100001011000110001001101000101
10001001010010000101001001110011101
110011011110110001111011101011011
1001101001110001010111001100001001
111001111010101000001011001000111
1010110110111111100010000101101011
10000010010011111010011001000100001
1100001101110111011110010110011001
1001001010011001100110110000110011
1101101111100110011010001001001101
101001001110110011001110011011101
1111011101100011001101011010011
10111001100010100110100000111101
1000101100100111110011100010111
1101000010111011101101010100011
10011100100011001100011111110101
1110101011010011001010111111
10110000000111100110000011111
100001000001011011001000101111
110001100010001000101101000111
1001010010011001101000011101011
1101111011100110011100101100001
1010011100101100110101100001001
111110101100001101000001000111
1011110000010010011100001101011
10001101000011011101010010100001
1101001110010100101111101111001
1001111010101111100011110011011
1110111000000111010101101101001
1011001010000101100000010001111
10000101111001000010000011010111
11001000110101100011000101000011
100101101010000010100100111100101
111000011111000011110111011011
1010100101110100101110011001001
111111100010111100010110010111
1011111010100011010100001100011
10001110111110100111110010010101
110101100111011110111010111
1010000011011001110011000011
1111000101000110101100100101
10110100111101010000010111
100001111011011111000100011
110010111001001110100110101
100110001010111010111101
1110010100000110000111
10101011110001001001011
100000001101001101110001
11000001001111010010101
1001000011101101111
1101100101100100111
10100011000010111011
11110100100100011001
10110111011011010011
100010011001000111101
1100111001011010111
10011010110001000011
11101000001001100101
101011100001110011
1000001010010101101
11000011111000001
10010010111010001
1101110001011101
101001010100011
111101111110101
101110011111
1000101101111
1101000100111
10011100111011
11101011011001
10110000100011
100001000110101
11000110101
10010101
111
1011
10001
1101
101
[TRUE]
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值