ホームページ  >  記事  >  Java  >  Javaの完全数

Javaの完全数

王林
王林オリジナル
2024-08-30 16:28:21657ブラウズ

Java は、ユーザーに異なる種類の番号体系を提供します。完全数も Java が提供する記数体系の 1 つです。 Java ではあらゆる数値を完全数とみなすことができます。数値を除くすべての要素が指定された数値と等しい場合、指定された数値は完全な数であると考えることができます。 Java では、さまざまな方法を使用して完全な数を見つけることができます。基本的に完全数とは、記数法において底が 10 である数にほかならず、数学における記数法の下位分野です。ユーザーの要件に応じて、完全数システムを使用できます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

完全数の背後にある論理

次に、次のように Java での完全数の背後にあるロジックを見てみましょう。

完全数の基本的なロジックは非常に単純です。まず、特定の数値の正の係数を見つけて、その数値自体を除くすべての係数の合計を計算する必要があります。因数の合計が指定された数と等しい場合、指定された数は完全な数であると言えます。因数の合計が指定された数と等しくない場合、指定された数は完全な数であると言えます。完全な数ではありません。完全数の例を見てみましょう。次に、次のような詳細なアイデアが得られます。

8 が完全数かどうかを確認する必要があるとします。

  • まず、正の因数 1、2、4、8 を見つけます。次に、8 を除くすべての因数を加算すると、結果は 7 になります。次に、結果を指定された数値と比較します。ここを参照してください。両方の数値が異なります。つまり、この数は完全な数ではないと言えます。
  • 次に、別の数字 6 を考えてみましょう。
  • 6 の正の因数が 1、2、3、6 であることを求めます。
  • すべての因数の合計は、6 を除いて 6 になります。
  • 次に、結果を指定された数値と比較します。ここで、指定された数値と結果が同じであることがわかります。これは、指定された数値が完全な数であると言えることを意味します。

Java で完全数を確認する方法?

それでは、次のように Java で完全数をチェックする方法を見てみましょう。 Java プログラミングでは、次の 3 つの異なる方法で完全数を確認できます。

1.  while ループを使用する

while ループでは、次のようないくつかの手順に従う必要があります。

1.まず、ユーザーから入力された番号を読み取る必要があります。

2.ループは、条件 (j

3.たとえば、no=8、j=1 no/2=4 なので、j

8%j=0 true の場合、合計 =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");
}
}

説明

上記のプログラムを使用して、Java で while ループを使用して完全数を実装しようとします。上記のプログラムのコーディングは非常に簡単で、ここでは main メソッドを作成しました。 main メソッド内では、スキャナー クラスと 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:ジャワのウェカ次の記事:ジャワのウェカ