关于正整数、质数与公因数及哥德巴赫猜想

前言:

一个正整数可以分成若干组整数乘式:
若规定乘式左侧数小于右侧数,则所有乘数的集合便是该数的公因数。
如:24=1X24;24=2X12;24=3X8;24=4X6,则24的公因数是1,2,3,4,6,8,12,24
若将左乘数当做白球放于白盒,右乘数当做黑球放于黑盒,则
P1:每一个白球与一个黑球之间存在唯一连接,使两球积为[源数]24。

P2:对于任何一个正整数E,E=1XE成立,所以白盒与黑盒存在必然元素。
P3:√E是白盒与黑盒的分界线,
白盒中的任意元素:Ew≤√E
黑盒中的任意元素:Eb≥√E

基于P1和P3,在寻找E的公因数时,只需寻找白盒中的所有元素,便可推出黑盒中的元素。寻找区间缩小至[2,√E]
将黑白盒的元素去重后便可得到E的所有公因数集合。
根据该集合元素个数,可将正整数数分为:
(1)既不是质数也不是合数:元素个数1
(2)素数(质数):元素个数2
(3)合数:元素个数≥2
9414344-ba3418c3a0fa21bf.png
黑白盒.png

编程实现:

功能:1.得到一个数的所有公因数
2.判断一个数是否是素数(质数)
3.取到范围内的所有素数
4.给出一个数的哥德巴赫猜想的表达式

/**
 * 作者:张风捷特烈
 * 时间:2018/8/24 0024:8:21
 * 邮箱:1981462002@qq.com
 * 说明:正整数集合
 */
public class PInt {

    private List<Integer> whiteBox = new ArrayList<>();//白盒
    private List<Integer> blackBox = new ArrayList<>();//黑盒

    private int mNum;//传入数据

    public PInt(int num) {
        mNum = num;
        if (num <= 0) {
            new RuntimeException("It is can't small than Zero");
        }
    }

    /**
     * 收集所有公因数
     *
     * @return
     */
    public List<Integer> collectBall() {
        collectWhiteBall();
        collectBlackBall();
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
        set.addAll(whiteBox);
        set.addAll(blackBox);
        return new ArrayList<>(set);
    }

    /**
     * 白盒收集公因数
     */
    private void collectWhiteBall() {
        whiteBox.add(1);
        double limitLine = Math.sqrt(mNum);//白盒边界最大值
        for (int i = 2; i <= limitLine; i++) {
            if (mNum % i == 0) {
                whiteBox.add(i);
            }
        }
    }

    /**
     * 用白盒映射出黑盒中公因数
     */
    private void collectBlackBall() {
        for (Integer i : whiteBox) {
            blackBox.add(mNum / i);
        }
    }

    /**
     * 判断是否是质数
     *
     * @return
     */
    public boolean isPrime() {
        return collectBall().size() == 2;
    }

    /**
     * 判断是否是质数
     *
     * @return
     */
    public static boolean isPrime(int num) {
        PInt pInt = new PInt(num);
        return pInt.collectBall().size() == 2;
    }

    /**
     * 获取区域内的所有质数(含起始)
     *
     * @param a 起始点
     * @param b 终止点
     * @return
     */
    public static ArrayList<Integer> getPrimeFromA2B(int a, int b) {

        ArrayList<Integer> list = new ArrayList<>();
        for (int i = a; i <= b; i++) {
            PInt pInt = new PInt(i);
            if (pInt.isPrime()) {
                list.add(i);
            }
        }
        return list;
    }

    /**
     * 一个数的哥德巴赫猜想所有可能表达式
     * @param num
     * @return
     */
    public static String guess(int num) {
        ArrayList<Integer> rightBox = new ArrayList<>();
        for (int i = 0; i <= num / 2; i++) {
            if (PInt.isPrime(i) && PInt.isPrime(num - i)) {
                rightBox.add(i);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (Integer i : rightBox) {
            sb.append(i + "+" + (num - i) + "=" + num + "
");
        }
        return sb.toString();
    }
}
测试类:公因数测试
public class Client {
    public static void main(String[] args) {
        PInt pInt = new PInt(510412736);
        System.out.println(pInt.collectBall());
        //[1, 2, 4, 8, 16, 32, 64, 510412736, 255206368
        //, 127603184, 63801592, 31900796, 15950398, 7975199]
    }
}
范围质数测试:10W数量级0.5s左右
public class Client {
    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        System.out.println(PInt.getPrimeFromA2B(0, 100000));
        long time1 = System.currentTimeMillis();
        System.out.println("time:" + (time1 - time)/1000.f + "秒");
    }
}
9414344-136b25d645e2d149.png
范围质数测试
哥德巴赫猜想测试
public class Client {
    public static void main(String[] args) {
        System.out.println(PInt.guess(124));
    }
}
9414344-8492dd0d47d69a34.png
哥德巴赫猜想测试.png

本文由张风捷特烈原创,转载请注明

原文地址:https://www.cnblogs.com/toly-top/p/9781952.html