检查数字是否是两个素数的总和

时间:2022-05-30 11:29:55

The problem is to check a random number n can be the sum of 2 random prime numbers. For example,

问题是检查随机数n可以是2个随机素数的总和。例如,

if n=34 the possibilities can be (3+31), (5+29), (17+17)...

如果n = 34,则可能是(3 + 31),(5 + 29),(17 + 17)......

So far I have managed to save prime numbers to the array, but have no clue how I could check, if n is the sum of 2 prime numbers.

到目前为止,我已设法将素数保存到数组中,但不知道如何检查,如果n是2个素数的总和。

This is part of my code:

这是我的代码的一部分:

public static void primeNumbers(int n) {
    int i = 0, candidate = 2, countArray = 0, countPrime = 0;
    boolean flag = true;

    while (candidate <= n) {
        flag = true;
        for (i = 2; i < candidate; i++) {
            if ((candidate % i) == 0) {
                flag = false;
                break;
            }
        }
        if (flag) {
            countPrime++;
        }
        candidate++;
    }
    int[] primeNumbers = new int[countPrime];


    while (candidate <= n) {
        flag = true;
        for (i = 2; i < candidate; i++) {
            if ((candidate % i) == 0) {
                flag = false;
                break;
            }
        }
        if (flag) {
            primeNumbers[countArray] = candidate;
        }
        candidate++;
        countArray++;
    }

    for (i = 0; i <= primeNumbers.length; i++) {

    }
}

First I counted how many prime numbers are between 1-n so I can declare and initialize my array for prime numbers. Then I save prime numbers to the array. But now I have no idea how I could check if n is the sum of 2 prime numbers.

首先,我计算了1-n之间有多少素数,因此我可以声明并初始化我的数组中的素数。然后我将素数保存到数组中。但现在我不知道如何检查n是否是2个素数的总和。

1 个解决方案

#1


2  

Given that you already have list of "prime numbers less than the given number", It is a very easy task to check if two prime numbers can sum to given number.

鉴于您已经有“质数小于给定数量”的列表,检查两个素数是否可以求和给定数字是一项非常容易的任务。

for(int i=0; i<array.length; i++){

    int firstNum = array[i];
    int secondNum = givenNum - firstNum;

    /* Now if it is possible to sum up two prime nums to result into given num, secondNum should also be prime and be inside array */

    if(ArrayUtils.contains(array, secondNum)){
        System.out.println("Yes, it is possible. Numbers are "+ firstNum + " and " + secondNum);
    }
}

EDIT: ArrayUtils is part of Apache Commons Lang library You can however use ArrayList instead to use contains method.

编辑:ArrayUtils是Apache Commons Lang库的一部分但是您可以使用ArrayList来使用contains方法。

#1


2  

Given that you already have list of "prime numbers less than the given number", It is a very easy task to check if two prime numbers can sum to given number.

鉴于您已经有“质数小于给定数量”的列表,检查两个素数是否可以求和给定数字是一项非常容易的任务。

for(int i=0; i<array.length; i++){

    int firstNum = array[i];
    int secondNum = givenNum - firstNum;

    /* Now if it is possible to sum up two prime nums to result into given num, secondNum should also be prime and be inside array */

    if(ArrayUtils.contains(array, secondNum)){
        System.out.println("Yes, it is possible. Numbers are "+ firstNum + " and " + secondNum);
    }
}

EDIT: ArrayUtils is part of Apache Commons Lang library You can however use ArrayList instead to use contains method.

编辑:ArrayUtils是Apache Commons Lang库的一部分但是您可以使用ArrayList来使用contains方法。