数字黑洞495,6174,...

    技术2022-08-01  100

    1955年,卡普耶卡(D.R.Kaprekar)研究了对四位数的一种变换:任给出四位数k0,用它的四个数字由大到小重新排列成一个四位数m,再减去它的反序数rev(m),得出数k1=m-rev(m),然后,继续对k1重复上述变换,得数k2.如此进行下去,卡普耶卡发现,无论k0是多大的四位数,只要四个数字不全相同,最多进行7次上述变换,就会出现四位数6174。因此这项研究在国际数学界又被称为“马丁猜想—6174问题”。

    有趣的数字6174

    随机生成四个不完全一样的数字(0000,1111,2222,等排除);四个数字组成一个最大的数 和 一个最小的数,如2,5,7,3组成的最大的数7532,最小的数2357;最大的数 - 最小的数,如果不等于6174,就按照上一步将差值重新组成一个最大的数 和 一个最小的数;最后一定有一次能得到差值为6174.俗称数字黑洞.

    JAVA实现

    public class Test6174 { public static void main(String[] args) throws InterruptedException { // 无线循环测试 while (true) { // 随机生成四个数字,考虑到出现四个一样的概率非常低 没有处理 List<Integer> meta = new ArrayList<>(); for (int i = 0; i < 4; i++) { meta.add(new Random().nextInt(10)); } System.out.print("原始数字:" + meta); int result = 0, count = 0; while (result != 6174) { // 获取四个数字组合的最大的数 和 最小的数 int max = getMax(meta); int min = getMin(meta); result = Math.abs(max - min); count++; // 数字为啥是6174? if (result == 6174) { System.out.println(",次数:" + count); } meta = getMeta(result); } Thread.sleep(1000); } } public static int getMax(List<Integer> meta) { List<Integer> tmp = meta.stream().sorted().collect(Collectors.toList()); return tmp.get(0) * 1000 + tmp.get(1) * 100 + tmp.get(2) * 10 + tmp.get(3); } public static int getMin(List<Integer> meta) { List<Integer> tmp = meta.stream().sorted().collect(Collectors.toList()); return tmp.get(0) + tmp.get(1) * 10 + tmp.get(2) * 100 + tmp.get(3) * 1000; } public static List<Integer> getMeta(int num) { List<Integer> tmp = new ArrayList<>(); tmp.add(num / 1000 % 10); tmp.add(num / 100 % 10); tmp.add(num / 10 % 10); tmp.add(num % 10); return tmp; } }

    同样的黑洞数字还有很多哦

    不如写个程序来找出不同位数的黑洞数字吧

    public class TestN { public static void main(String[] args) throws InterruptedException { // 输入几位数就是找几位数的黑洞数字 int digits = 5; List<Integer> meta = new ArrayList<>(); for (int i = 0; i < digits; i++) { meta.add(new Random().nextInt(10)); } if (getMax(meta) - getMin(meta) == 0){ System.out.println("数字完全一样,不符合要求"); System.exit(0); } System.out.println("原始数字:" + meta); int result = 0; while (true) { int max = getMax(meta); int min = getMin(meta); result = Math.abs(max - min); meta = getMeta(result, digits); System.out.println(result); } } // 获取数字组合的最大数 public static int getMax(List<Integer> meta) { List<Integer> tmp = meta.stream().sorted().collect(Collectors.toList()); double result = 0; for (int i = 0; i < tmp.size(); i++) { result = result + tmp.get(i) * (Math.pow(10.0, (double) (i))); } return (int) result; } // 获取数字组合的最小数 public static int getMin(List<Integer> meta) { List<Integer> tmp = meta.stream().sorted().collect(Collectors.toList()); double result = 0; for (int i = 0; i < tmp.size(); i++) { result = result + tmp.get(i) * (Math.pow(10.0, (double) (tmp.size() - i - 1))); } return (int) result; } // 获取数的各位数 public static List<Integer> getMeta(int num, int c) { List<Integer> tmp = new ArrayList<>(); for (int i = 0; i < c; i++) { tmp.add(num / ((int) Math.pow(10.0, i)) % 10); } return tmp; } }

    如上程序测试运行:

    digits(位数)黑洞数(个)结果1--259,81,63,27,45314954161745471973,83952,74943,6296467840852,860832,862632,642654,...789529641,8719722,8649432,7519743,...8364308654,83208762,86526432914954197541,883098612,976494321,...
    Processed: 0.010, SQL: 9