首页  >  文章  >  Java  >  Java 中的完美数

Java 中的完美数

王林
王林原创
2024-08-30 16:28:21656浏览

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
上一篇:Weka in Java下一篇:Smith Number in Java