java - 寻找一个奇完全数

标签 java methods numbers perfect-numbers

我编写了这两个方法来确定一个数字是否完美。我的教授希望我将它们结合起来,看看是否存在奇完全数。我知道没有(已知的),但我需要实际编写代码来证明这一点。

问题出在我的主要方法上。我测试了两种测试方法。我尝试调试,结果卡在数字 5 上,但我不明白为什么。这是我的代码:

public class Lab6
{
public static void main (String[]args)
{
  int testNum = 3;

  while (testNum != sum_of_divisors(testNum) && testNum%2 != 2)
     testNum++;

}

public static int sum_of_divisors(int numDiv)
{
  int count = 1;
  int totalDivisors = 0;

  while (count < numDiv)
     if (numDiv%count == 0)
     {
        totalDivisors = totalDivisors + count;
        count++;
     }
     else
     count++;

  return totalDivisors;
}

public static boolean is_perfect(int numPerfect)
{
  int count = 1;
  int totalPerfect = 0;

  while (totalPerfect < numPerfect)
  {
     totalPerfect = totalPerfect + count;
     count++;
  }
  if (numPerfect == totalPerfect)
     return true;
  else
     return false;
}
}

最佳答案

制作

testNum%2 != 2

作为

testNum%2 != 0

关于java - 寻找一个奇完全数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19673433/

相关文章:

java - hibernate中的自引用实体会导致StackOverflowErrors

C++类成员变量重新赋值

c++ - 在 C++ 中以特定的间隔速率调用方法

java - 自守数 - Java 程序

c - 如何在 C 中使用逗号作为千位分隔符来格式化数字?

java - Java的java.util.Random靠谱吗?

java - 在运行时从类中调用方法

java - 链接列表对象不存储字符串输入,这会导致链接列表为空

methods - 从转换为具有 asType 的类的 Groovy 映射中调用方法

language-agnostic - 为什么大多数语言不允许二进制数?