考拉兹猜想:一个正整数x,如果是奇数就乘以3再加1,如果是偶数就析出偶数因数2ⁿ,这样经过若干个次数,最终回到1。
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向小值收敛。
*/
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 = 27L;
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 = [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]
arr_size = 110
distributeArr = [3, 2, 3, 2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 3, 2, 2, 3, 2, 3, 2, 2, 3, 2, 3, 2, 3, 2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 3, 2, 2, 2, 3, 2, 3, 2, 3, 2, 2, 3, 2, 3, 2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 3, 2, 3, 2, 3, 2, 2, 2, 2, 2, 3, 2, 2, 2]
distributeArr_size = 110
ch2 = 69
ch3 = 41
p_ch2 = 0.6272727
p_ch3 = 0.37272727
ch2_divide_ch3 = 1.6829268
v = 1.584962500721156
ch2_divide_ch3 > v 故数列必收敛
TODO 未完待完善 2021-9-12