考拉兹猜想的概率解释

考拉兹猜想:一个正整数x,如果是奇数就乘以3再加1,如果是偶数就除以2,这样经过若干个次数,最终回到1。
如正整数6: 6/2=3,3x3+1=10,10/2=5,5x3+1=16,16/2=8,8/2=4,4/2=2,2/2=1,无论这个过程中的数值如何庞大,在经过若干次的变换之后也必然会到:4-2-1的循环。

考拉兹猜想java代码实现:

package com.yqq.algorithm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *  考拉兹猜想的概率解释:
 *      create by yqq
 *      date on 2021-9-4
 *      code for 考拉兹猜想
 *      ch2函数密度为2/3
 *      ch3函数密度为1/3
 *      if limit -> infinite : count(ch2)/count(ch3) -> 2
 *      对正整数k,作用偶变换f(n) = 2^n 以及 作用奇变换 g(m) = 3^m 有:
 *      k 除以 (2^n) 乘以 (3^m) -> 1
 *      需要 2^n > 3^m
 *      令:
 *      2^n = 3^m
 *      nlg2 = mlg3
 *      n/m = lg3/lg2 = 1.585
 *      有n必须是m的1.585倍以上k向1收敛。
 */
public class CollatzConjecture {

    private static List<Long> arr = new ArrayList<>();
    private static List<Integer> distributeArr = new ArrayList<>();
    private static long ch2 = 0;
    private static long ch3 = 0;

    public static void main(String[] args) throws IOException {
        long n = 999999991L;
        chang(n);
    }

    private static void chang3(long k) {
        long i = k * 3 + 1;
        ch3++;
        distributeArr.add(3);
        chang(i);
    }

    private static void chang2(long k) {
        long i = k / 2;
        ch2++;
        distributeArr.add(2);
        if (i == 2) {
            System.out.println("i = " + i);
            //arr.add(i);
            System.out.println("arr = " + arr);
            System.out.println("arr_size = " + arr.size());
            System.out.println("distributeArr = " + distributeArr);
            System.out.println("distributeArr_size = " + distributeArr.size());

            System.out.println("ch2 = " + ch2);
            System.out.println("ch3 = " + ch3);
            System.out.println("p_ch2 = " + (float) ch2 /distributeArr.size());
            System.out.println("p_ch3 = " + (float) ch3 /distributeArr.size());
            float ch2_divide_ch3 = (float) ch2 / (float) ch3;
            System.out.println("ch2_divide_ch3 = " + ch2_divide_ch3);
            double v = Math.log10(3) / Math.log10(2);
            System.out.println("v = " + v);
            System.out.println("ch2_divide_ch3 > v 故数列必收敛");
            System.exit(1);
        }
        chang(i);
    }

    private static void chang(long m){
        arr.add(m);
        if (m % 2 == 0) {
            // 偶变换
            chang2(m);
        }else {
            // 奇变换
            chang3(m);
        }
    }
}

运行结果:

i = 2
arr = [999999991, 2999999974, 1499999987, 4499999962, 2249999981, 6749999944, 3374999972, 1687499986, 843749993, 2531249980, 1265624990, 632812495, 1898437486, 949218743, 2847656230, 1423828115, 4271484346, 2135742173, 6407226520, 3203613260, 1601806630, 800903315, 2402709946, 1201354973, 3604064920, 1802032460, 901016230, 450508115, 1351524346, 675762173, 2027286520, 1013643260, 506821630, 253410815, 760232446, 380116223, 1140348670, 570174335, 1710523006, 855261503, 2565784510, 1282892255, 3848676766, 1924338383, 5773015150, 2886507575, 8659522726, 4329761363, 12989284090, 6494642045, 19483926136, 9741963068, 4870981534, 2435490767, 7306472302, 3653236151, 10959708454, 5479854227, 16439562682, 8219781341, 24659344024, 12329672012, 6164836006, 3082418003, 9247254010, 4623627005, 13870881016, 6935440508, 3467720254, 1733860127, 5201580382, 2600790191, 7802370574, 3901185287, 11703555862, 5851777931, 17555333794, 8777666897, 26333000692, 13166500346, 6583250173, 19749750520, 9874875260, 4937437630, 2468718815, 7406156446, 3703078223, 11109234670, 5554617335, 16663852006, 8331926003, 24995778010, 12497889005, 37493667016, 18746833508, 9373416754, 4686708377, 14060125132, 7030062566, 3515031283, 10545093850, 5272546925, 15817640776, 7908820388, 3954410194, 1977205097, 5931615292, 2965807646, 1482903823, 4448711470, 2224355735, 6673067206, 3336533603, 10009600810, 5004800405, 15014401216, 7507200608, 3753600304, 1876800152, 938400076, 469200038, 234600019, 703800058, 351900029, 1055700088, 527850044, 263925022, 131962511, 395887534, 197943767, 593831302, 296915651, 890746954, 445373477, 1336120432, 668060216, 334030108, 167015054, 83507527, 250522582, 125261291, 375783874, 187891937, 563675812, 281837906, 140918953, 422756860, 211378430, 105689215, 317067646, 158533823, 475601470, 237800735, 713402206, 356701103, 1070103310, 535051655, 1605154966, 802577483, 2407732450, 1203866225, 3611598676, 1805799338, 902899669, 2708699008, 1354349504, 677174752, 338587376, 169293688, 84646844, 42323422, 21161711, 63485134, 31742567, 95227702, 47613851, 142841554, 71420777, 214262332, 107131166, 53565583, 160696750, 80348375, 241045126, 120522563, 361567690, 180783845, 542351536, 271175768, 135587884, 67793942, 33896971, 101690914, 50845457, 152536372, 76268186, 38134093, 114402280, 57201140, 28600570, 14300285, 42900856, 21450428, 10725214, 5362607, 16087822, 8043911, 24131734, 12065867, 36197602, 18098801, 54296404, 27148202, 13574101, 40722304, 20361152, 10180576, 5090288, 2545144, 1272572, 636286, 318143, 954430, 477215, 1431646, 715823, 2147470, 1073735, 3221206, 1610603, 4831810, 2415905, 7247716, 3623858, 1811929, 5435788, 2717894, 1358947, 4076842, 2038421, 6115264, 3057632, 1528816, 764408, 382204, 191102, 95551, 286654, 143327, 429982, 214991, 644974, 322487, 967462, 483731, 1451194, 725597, 2176792, 1088396, 544198, 272099, 816298, 408149, 1224448, 612224, 306112, 153056, 76528, 38264, 19132, 9566, 4783, 14350, 7175, 21526, 10763, 32290, 16145, 48436, 24218, 12109, 36328, 18164, 9082, 4541, 13624, 6812, 3406, 1703, 5110, 2555, 7666, 3833, 11500, 5750, 2875, 8626, 4313, 12940, 6470, 3235, 9706, 4853, 14560, 7280, 3640, 1820, 910, 455, 1366, 683, 2050, 1025, 3076, 1538, 769, 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]
arr_size = 347
distributeArr = [3, 2, 3, 2, 3, 2, 2, 2, 3, 2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 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对正整数999999991按照考拉兹运算得到有限数组arr,数组arr最后一位是4,即回到4-2-1。
数组arr大小为347,偶变换ch2有224次,奇变换ch3有123次。ch2除以ch3等于1.8211383大于lg3/lg2。(*对任何n都有ch2/ch3大于lg3/lg2)

考拉兹猜想的概率解释

考拉兹猜想的概率解释

上一篇:BTrace:线上问题排查工具


下一篇:基于STM32设计的健康检测设备(测温心率计步)