Java程式檢查給定數字是否為完美數


當給定數字的因子之和(去除給定數字本身)等於該數字本身時,稱為完美數。這些約數的和稱為真約數和。因此,如果一個數與其真約數和相等,則該數是完美數。值得注意的是,所有已知的完美數都是偶數。

問題陳述

在這篇文章中,我們將建立Java程式來檢查給定數字是否為完美數。對於給定的問題,我們將使用迭代方法,例如for迴圈while迴圈。讓我們透過一些例子來理解:

輸入1

Given number: 496

輸出1

Its factors are: 1, 2, 4, 8, 16, 31, 62, 124, and 248 ( we have to exclude 496 )

Sum of the factors are: 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496

Therefore, it is a perfect number

輸入2

Given number: 54

輸出2

Its factors are: 1, 2, 3, 6, 9, 18, and 27 ( we have to exclude 54 )

Sum of the factors are: 1 + 2 + 3 + 6 + 9 + 18 + 27 = 66

Therefore, it is not a perfect number

檢查完美數的方法

以下是我們可以用來檢查給定數字是否為完美數的一些方法:

使用For迴圈檢查完美數

for迴圈是入口控制迴圈,其中執行給定條件。

語法

for ( initial expression; conditional expression; increment/decrement expression )
{
   // code to be executed
}

初始表示式 - 迴圈開始時執行一次。

條件表示式 - 直到條件表示式為真時才會執行程式碼。

增量/減量表達式 - 用於增量/減量迴圈變數。

使用for迴圈檢查給定數字是否為完美數的步驟

以下是使用for迴圈檢查給定數字是否為完美數的步驟:

  • java.util包匯入類。
  • 宣告並初始化一個整數變數n1表示要檢查的數字,另一個整數變數add用於儲存其因子的和。
  • 使用一個for迴圈,從1執行到n1 - 1
  • 在迴圈內,使用if語句檢查n1是否可以被當前迴圈變數整除。
  • 如果可以整除,則將迴圈變數新增到add中。
  • 迴圈結束後,使用if-else塊檢查add是否等於n1

示例

import java.util.*;
public class Perfect {
   public static void main(String[] args) {
      int n1 = 496;
      int add = 0;
      for(int i = 1; i < n1; i++) {
         if(n1 % i==0) {
            add = add + i;  
            // adding and incrementing 
         }
      }
      boolean isPerfect = (add == n1);
      if(isPerfect) {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      } else {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      }
   }    
}

輸出

is 496 a perfect number?: true 

使用While迴圈檢查完美數

while迴圈是入口控制迴圈,在執行迴圈體之前檢查條件。

語法

while (conditional expression) {
   // code will be executed till conditional expression is true
   increment/decrement expression; 
   // to increment or decrement loop variable
}

使用while迴圈檢查給定數字是否為完美數的步驟

以下是使用while迴圈檢查給定數字是否為完美數的步驟:

  • java.util包匯入類。
  • 宣告並初始化一個整數變數n1表示要檢查的數字,另一個整數變數add用於儲存其因子的和。
  • 宣告一個迴圈變數i並將其設定為1
  • 使用一個while迴圈,只要i小於n1就執行。
  • 在迴圈內,使用if語句檢查n1是否可以被i整除。如果可以整除,則將i新增到add中。
  • i遞增1。迴圈結束後,使用if-else塊檢查add是否等於n1

示例

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int n1 = 28;
      int add = 0;
      int i = 1;  
      // loop variable
      while(i < n1) {
         if(n1 % i == 0) {
            add = add + i; 
         }
         i++; 
         // incrementing 
      }
      boolean isPerfect = (add == n1);
      if(isPerfect) {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      } else {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      }
   }    
}

輸出

is 28 a perfect number?: true

在上面的程式中,我們遵循相同的邏輯,但使用了不同的變數n1值,並且使用了while迴圈而不是for迴圈。

迴圈到n/2檢查完美數

這種方法比我們在本文前面討論的另外兩種方法更最佳化。在這種方法中,迴圈將僅迭代到給定數字的一半,因為我們可以在該數字的一半之間找到該數字的所有因子(不包括數字本身)。

迭代到n/2檢查給定數字是否為完美數的步驟

以下是迭代到n/2檢查給定數字是否為完美數的步驟:

  • 宣告並初始化一個整數變數n1表示要檢查的數字,另一個整數變數add用於儲存其因子的和。
  • 宣告一個迴圈變數i並將其設定為1
  • 使用while迴圈,只要i小於或等於n1 / 2就執行。
  • 在迴圈內,使用if語句檢查n1是否可以被i整除。如果可以整除,則將i新增到add中。將i遞增1
  • 迴圈結束後,使用if-else塊檢查add是否等於n1

示例

import java.util.*;
public class Perfect {
   public static void main(String[] args) {
      int n1=6;
      int add = 0;
      int i=1;
      while(i <= n1/2) { 
         // loop will run till 3 ( 6/2 = 3)
         if(n1 % i==0) {
            add = add + i;
         }
         i++;
      }
      boolean isPerfect = (add == n1);
      if(isPerfect) {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      } else {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      }
   }    
}

輸出

is 6 a perfect number?: true

結論

在這篇文章中,我們看到了三種Java程式方法來檢查給定數字是否為完美數。我們瞭解瞭如何使用迭代方法來編寫Java程式。方法3更最佳化,我們推薦使用這種方法。

更新於:2024年8月5日

2K+ 瀏覽量

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.