搜尋
首頁Javajava教程Java 中的完美數

Java 中的完美數

Aug 30, 2024 pm 04:28 PM
java

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
<p><strong>說明</strong></p>
<p>透過使用上面的程序,我們嘗試用 while 迴圈在 java 中實現完美數。上面程式的編碼非常簡單,這裡我們建立了main 方法;在main 方法中,我們使用Scanner 類別和while 迴圈來尋找給定數字的因子,並將該因子加入s 變數中,該變數是因子的總和,如上面的程序所示。最後,我們比較兩個數字並根據比較列印訊息。我們使用下面的螢幕截圖來說明上述程式的最終輸出。 </p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500650337359.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的完美數" ></p>
<h4 id="範例">範例#2</h4>
<p>現在讓我們來看另一個使用靜態方法的完全數字範例,如下所示。 </p>
<p><strong>代碼:</strong></p>
<pre class="brush:php;toolbar:false">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
<p><strong>說明</strong></p>
<p>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.</p>


<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500650473605.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的完美數" ></p>
<h4 id="Example">Example #3</h4>
<p>Now let’s see another example to check perfect numbers by using the recursive Method as follows.</p>
<p><strong>Code:</strong></p>
<pre class="brush:php;toolbar:false">public class Recursive
{
static int n = 200;
static int s = 0;
static int d = 1;
static int findPerfect(int n, int d) {
{
if(d
<p><strong>Explanation</strong></p>
<p>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.</p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500650667330.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的完美數" ></p>
<h3 id="Conclusion">Conclusion</h3>
<p>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.</p>

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

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。