首頁 > Java > java教程 > 主體

在Java中如何檢查一個數字是否為哥德巴赫數?

王林
發布: 2023-08-26 17:25:14
轉載
692 人瀏覽過

在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中如何檢查一個數字是否為哥德巴赫數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!