首頁  >  文章  >  Java  >  Java 中的完美數

Java 中的完美數

王林
王林原創
2024-08-30 16:28:21657瀏覽

Java為使用者提供了不同類型的數字系統;完全數也是java提供的一種數字系統。在Java中我們可以將任何數字視為完美數。如果除它之外的所有因數都等於給定數,那麼我們可以認為給定數是完全數。在Java中,我們可以使用不同的方法來找到完美數。基本上,完美數只不過是數系中以 10 為底的任何數字,它是數學中數系的一個子領域。根據使用者要求,我們可以使用完美的數字系統。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

完美數字背後的邏輯

現在讓我們來看看java完美數背後的邏輯如下。

完美數的基本邏輯非常簡單。首先,我們需要找出給定數字的正因數,然後將除該數字本身之外的所有因數相加。如果因數和等於給定數,則可以說給定數是完全數,如果因數和不等於給定數,則可以說給定數是不是完美的數字。讓我們來看一個完美數的例子;那麼我們將得到如下詳細的想法。

假設我們需要檢查 8 是否為完美數。

  • 先找出正因數1、2、4、8。現在將除8之外的所有因數相加,得到的結果是7。現在將結果與給定的數字進行比較;看到這裡兩個數字是不同的。這意味著我們可以說這個數字不是一個完美的數字。
  • 現在讓我們考慮另一個數字:6。
  • 求 6 的正因數是 1、2、3、6。
  • 所有因數和為 6,排除 6。
  • 現在將結果與給定的數字進行比較,看到這裡給定的數字和結果是相同的,這意味著我們可以說給定的數字是一個完美的數字。

如何在Java中檢查完全數?

現在讓我們看看如何檢查完全數java,如下所示。在java程式設計中,有以下三種不同的方法來檢查完全數。

1.  透過使用 while 迴圈

在while循環中,我們需要執行以下一些步驟。

1.首先,我們需要讀取使用者輸入的號碼。

2.循環將繼續,直到條件 (j

3.例如 no=8, j=1 no/2=4,所以 j

8%j=0 true 那麼 sum =1

J=2 2

這樣,我們就完成了所有的迭代,找到了完美的數字。

2.透過使用靜態方法

在這個方法中,我們可以呼叫靜態方法來檢查完美數;在這個方法中,我們只需要呼叫PerfacOrNot方法。它會自動計算所有正因數的總和,並檢查給定的數字是否完美。

3.透過使用遞歸方法

在這個方法中,我們也使用物件呼叫了 PerfectOrNot() 方法。在此方法中,執行自行啟動並呼叫 PerfectOrNot ()。它重複迭代,直到 j

範例

現在讓我們來看看 Java 中完美數的不同範例,如下。

範例#1

代碼:

import java.util.Scanner;
class Perfect_number1
{
public static void main(String arg[])
{
long num,s=0;
Scanner s_c=new Scanner(System.in);
System.out.println("Enter number");
num=s_c.nextLong();
int j=1;
while(j<=num/2)
{
if(num%j==0)
{
s+=j;
}
j++;
}
if(s==num)
{
System.out.println(num+" the given number is perfect number");
}
else
System.out.println(num+" the given number is not perfect number");
}
}

說明

透過使用上面的程序,我們嘗試用 while 迴圈在 java 中實現完美數。上面程式的編碼非常簡單,這裡我們建立了main 方法;在main 方法中,我們使用Scanner 類別和while 迴圈來尋找給定數字的因子,並將該因子加入s 變數中,該變數是因子的總和,如上面的程序所示。最後,我們比較兩個數字並根據比較列印訊息。我們使用下面的螢幕截圖來說明上述程式的最終輸出。

Java 中的完美數

範例#2

現在讓我們來看另一個使用靜態方法的完全數字範例,如下所示。

代碼:

import java.util.Scanner;
class Perfect_Method
{
public static void main(String arg[])
{
long num,m;
Scanner s_c=new Scanner(System.in);
System.out.println("Enter number");
num=s_c.nextLong();
m=perfectOrNot(num);
if(m==num)
System.out.println(num+" The given number is perfect number");
else
System.out.println(num+" The given number is not perfect number");
}
static long perfectOrNot(long n)
{
long s=0;
for(int j=1;j<=n/2;j++)
{
if(n%j==0)
{
s+=j;
}
}
return s;
}
}

說明

In the above program, we use a static method to check if a given number is a perfect number or not. In the above program, we use the perfecOrNot () method. After that, we use a for loop to find the factors of the given number, and the remaining process is the same, that is, to compare the result with the given number and print a message according to the comparison. The final output of the above program we illustrate by using the following screenshot as follows.

Java 中的完美數

Example #3

Now let’s see another example to check perfect numbers by using the recursive Method as follows.

Code:

public class Recursive
{
static int n = 200;
static int s = 0;
static int d = 1;
static int findPerfect(int n, int d) {
{
if(d<=n/2)
{
if(n%d==0)
{
s+=d;
}
d++;
findPerfect(n,d);
}
return s;
}
}
public static void main(String args[])
{
int r = findPerfect(n,d);
if(r == n)
System.out.println(" The given number is perfect Number");
else System.out.println("The given number is not perfect Number");
}
}

Explanation

In the above program, we are using a recursive method to check the perfect number. The final output of the above program we illustrate by using the following screenshot as follows.

Java 中的完美數

Conclusion

We hope from this article you learn Perfect Number in java. From the above article, we have learned the basic logic of Perfect Numbers, and we also see different examples of Perfect Numbers. From this article, we learned how and when we use the Perfect Number in java.

以上是Java 中的完美數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java中的Weka下一篇:Java中的Weka