Java 如何检查一个数字是否是Goldbach数
如果一个数字可以表示为两个奇素数对的相加,那么它被称为 Goldbach 数。
如果我们遵循上述条件,我们可以发现每个大于4的偶数都是Goldbach数,因为它必定有一对奇素数对。但奇数不符合条件,因为我们知道两个数的相加永远不会得到奇数。
在本文中,我们将了解如何使用Java编程语言来检查一个数字是否是Goldbach数。
一些实例
示例1
输入数字为50。
让我们用Goldbach数的逻辑来检查它。
找到奇素数对,我们得到:
(3 , 47)
(7 , 43)
(13 , 37)
(19 , 31)
正如我们在这里所注意到的,我们得到了一些奇质数对,它们的相加值等于50。
因此,50是一个哥德巴赫数字。
示例2
输入数字为47。
让我们使用哥德巴赫数字的逻辑来检查它。
寻找奇质数对,我们得到- 没有可用的对。
正如我们在这里注意到的,我们没能得到任何一对奇质数,它们的相加值等于47。
因此,47不是一个哥德巴赫数字。
其他哥德巴赫数的示例包括20, 52, 48, 122等。
步骤
- 步骤1 - 通过初始化或用户输入获得一个整数。
-
步骤2 - 然后声明两个连续存储素数的数组。
-
步骤3 - 然后在迭代中开始查找两个奇质数对,它们的相加值与输入数字相同。
-
步骤4 - 如果我们找不到任何奇质数对,那么我们可以打印出给定的数字不是一个哥德巴赫数。
-
步骤5 - 如果我们得到了一些对,那么我们只需打印出这些对以及结果消息,即输入数字是一个哥德巴赫数字。
多种方法
我们提供了不同的方法来解决问题。
- 通过使用静态输入值
-
通过使用用户定义的方法
让我们一次看到程序及其输出。
方法1:通过使用静态输入值
在这种方法中,程序将初始化一个整数值,然后通过使用算法,我们可以检查一个数字是否是 哥德巴赫 数。
示例
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) {
//declare all the variables
int i, j, n, temp, b=0, c=0, sum=0;
//declare a variable which stores the input number
//assign a value to it
int inputNumber=30;
//declare a temporary variable which stores the input value
temp=inputNumber;
//declare two arrays with the capacity equal to input number
int array1[]=new int[inputNumber];
int array2[]=new int[inputNumber];
//check whether the number is even or
if(inputNumber%2!=0) {
//if the input is not even then print it is not a Goldbach number
System.out.println(inputNumber + " is not a Goldbach number.");
}
//if the input is even then proceed with further calculations
else {
//initiate the loop for finding the prime numbers
for(i=1; i<=inputNumber; i++) {
for(j=1; j<=i; j++) {
if(i%j==0) {
c++;
}
}
//find the odd prime numbers
if((c==2)&&(i%2!=0)) {
//stores odd prime numbers into first array
array1[b]=i;
//stores odd prime numbers into second array
array2[b]=i;
//increments the value of b by 1
b++;
}
c=0;
}
//print the odd prime number pairs
System.out.println("Odd Prime Pairs are: ");
//loop for printing the value of ArrayStoreException
for(i=0; i<b; i++) {
for(j=i; j<b; j++) {
//find the sum of two odd prime numbers
sum=array1[i]+array2[j];
//condition for comparing the sum value with input number
if(sum==temp) {
//print pair of odd prime numbers
System.out.print("(" + array1[i]+" , "+array2[j] + ")");
System.out.println();
}
}
}
//print the final result if it is Goldbach number
System.out.println(temp+" is a Goldbach number.");
}
}
}
输出
Odd Prime Pairs are:
(7 , 23)
(11 , 19)
(13 , 17)
30 is a Goldbach number.
方法2:通过使用用户自定义方法
在这个方法中,我们先初始化一个整数值,然后通过将该输入数字作为参数调用一个用户自定义的方法。
在方法内部,我们将使用算法来检查一个数字是否为 哥德巴赫 数。
示例
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) {
//declare a variable which stores the input number
//assign a value to it
int inp=98;
if(checkGoldbach(inp)) {
//if true it is Goldbach number
System.out.println(inp+" is a Goldbach number.");
} else {
//if false it is not a Goldbach number
System.out.println(inp + " is not a Goldbach number.");
}
}
//define the user defined method
static boolean checkGoldbach(int inputNumber) {
//declare all the variables
int i, j, n, temp, b=0, c=0, sum=0;
//declare a temporary variable which stores the input value
temp=inputNumber;
//declare two arrays with the capacity equal to input number
int array1[]=new int[inputNumber];
int array2[]=new int[inputNumber];
//check whether the number is even or
if(inputNumber%2!=0) {
return false;
}
//if the input is even then proceed with further calculations
else {
//initiate the loop for finding the prime numbers
for(i=1; i<=inputNumber; i++) {
for(j=1; j<=i; j++) {
if(i%j==0) {
c++;
}
}
//find the odd prime numbers
if((c==2)&&(i%2!=0)) {
//stores odd prime numbers into first array
array1[b]=i;
//stores odd prime numbers into second array
array2[b]=i;
//increments the value of b by 1
b++;
}
c=0;
}
//print the odd prime number pairs
System.out.println("Odd Prime Pairs are: ");
//loop for printing the value of Arrays
for(i=0; i<b; i++) {
for(j=i; j<b; j++) {
//find the sum of two odd prime numbers
sum=array1[i]+array2[j];
//condition for comparing the sum value with input number
if(sum==temp) {
//print pair of odd prime numbers
System.out.print("(" + array1[i]+" , "+array2[j] + ")");
System.out.println();
}
}
}
return true;
}
}
}
输出
Odd Prime Pairs are:
(19 , 79)
(31 , 67)
(37 , 61)
98 is a Goldbach number.
在本文中,我们探讨了如何使用三种不同的方法在Java中检查一个数字是否为哥德巴赫数。