如何在 Java 中檢查一個數是否為哥德巴赫數?
如果一個數可以表示為兩個奇質數的和,則稱該數為哥德巴赫數。
如果我們遵循上述條件,那麼我們可以發現大於 4 的所有偶數都是哥德巴赫數,因為它們一定存在一對奇質數的和。但是奇數不滿足,因為我們知道兩個數的和永遠不可能是奇數。
在本文中,我們將瞭解如何使用 Java 程式語言來檢查一個數是否為哥德巴赫數。
舉一些例子
示例 1
輸入數字為 50。
讓我們使用哥德巴赫數的邏輯來檢查它。
找到奇質數對,我們得到
(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 中檢查一個數是否為哥德巴赫數。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP