1

我应该使用以下伪代码创建一个完美的数字类:

For i from 2 to “very large”,
    For j from 2 to √i,
         if (j evenly divides i),
              accumulate the sum j and i/j
    if √i is an integer
         subtract √i ... you added it twice
    if the sum of divisors == i
         Print the number ... it’s perfect!

所以这是我的版本。它运行,但它根本不做我想要的。它只是运行并且不产生任何输出。有人可以告诉我我的程序有什么问题吗?这让我很困扰。

import java.util.Scanner;

public class PerfectNumber {

public static void main(String[] args) {
  double sum = 0
  double newsum = 0;
  for (int i = 2; i < 1000000; i++) {
     for (int j = 2; i<Math.sqrt(i); j++){
        if (i%j==0){
           sum = j + (i%j);

        }
        if (Math.sqrt(i)==(int)i){ 
        newsum = sum - Math.sqrt(i);
        }   
        if (sum == 0) {
        System.out.println(sum + "is a perfect number");
        }

}
}
}
}
4

7 回答 7

2

根据算法的几个错误:

  1. sum = j + (i%j);应该改为sum = j + (i/j);

  2. 这件作品:

    if (Math.sqrt(i)==(int)i){ 
        newsum = sum - Math.sqrt(i);
    }   
    if (sum == 0) {
        System.out.println(sum + "is a prime number");
    }
    

应该在上面的“for”之下

  1. Math.sqrt(i)==(int)i除非 i 是 1,否则永远不会是真的。如果你想这样检查,你应该写Math.sqrt(i)==((int) Math.sqrt(i))

还有更多错误,最简单的方法是:

double sum = 0;
for (int i = 1; i <= 10000; i++) {
    for (int j = 1; j < i; j++) {
        if (i % j == 0) {
            sum += j;
        }
    }
    if (i == sum) {
        System.out.println(sum + " is a prime number");
    }
    sum = 0;
}
于 2015-04-30T19:17:07.060 回答
1

您的代码包含几个错误。这是更正后的代码,对更改进行了注释。

// newsum isn't needed; declare sum to be int to avoid floating-point errors
int sum = 0;
for (int i = 2; i < 1000000; i++) {
    // Start with 1; every natural number has 1 as a factor.
    sum = 1;
    // Test if j, not i, is less than the square root of i.
    for (int j = 2; j <= Math.sqrt(i); j++){
        if (i % j == 0){
            // Add to sum; don't replace sum.  Use i / j instead of i % j.
            sum = sum + j + (i / j);
            // Move test inside this if; test if j is square root of i
            if (j*j == i){
                // I used j because we know it's the square root already.
                sum = sum - j;
            }
        }
        // Move print outside of inner for loop to prevent multiple 
        // printings of a number.
        // Test if sum equals the number being tested, not 0.
        if (sum == i) {
             // Space before is
             System.out.println(sum + " is a perfect number");
        }
    }
}

输出:

6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
于 2015-04-30T19:30:38.303 回答
1
public static void main(String[] args){
    int min = 2; 
    int max = 1000000;
    int sum = 0;
    for (; min <= max; min++,sum = 0) { 
        for (int e = 1; e < min; e++)
            sum += ((min % e) == 0) ? e : 0;

        if (sum == min){           
            System.out.println(sum);
        }          
    }      
}
于 2015-09-17T16:11:55.637 回答
0

根据您想要将第二个和第三个 if test 移到内部循环之外的伪代码

for (int i = 2; i < 1000000; i++) {
    double iroot = Math.sqrt(i);
    int sum = 1;
    for (int j = 2; j <= iroot; j++){
        if (i % j == 0){
            sum = sum + j + i / j;
        }
    }
    if (iroot == (int) iroot) {
        sum = sum - iroot;
    }
    if (sum == i) {
        System.out.println(sum + "is a perfect number");
    }
}
于 2015-04-30T19:43:49.210 回答
0

这是您可以为完美数编写程序的最简单和最简单的形式……此代码给出了 25 以内的完美数……您可以根据需要进行更改

import java.util.Scanner;
public class PerfectNumber {
    public static void main(String[] args) {
        int n,i,j,count=0;

        for(i=2;i<=25;i++) {
            for(j=1;j<=i;j++) {
                if(i%j ==0)    /*count increments if a reminder zero*/ {
                    count++;
                }
            }
            /*since a perfect number is divided only by 1 and itself
              if the count is 2 then its a prime number...*/

            if(count==2)
                System.out.println(i);
            count=0;
        }
        return 0;
    }
}
于 2015-09-17T16:35:12.137 回答
0
for(n=1;n<=number;n++){ //calculates the sum of the number.
 int i=1;
 int sum = 0;
  while(i<n){
    if(n%i==0)
         sum+=i;
        i++;
  }
        if(sum==n){ //if the sum is equal to its sum :
          System.out.print(n+": ");
          for (int j = 1;j<n;j++){
              if(n%j==0){
              System.out.print(j+" ");
          }
          }
          System.out.println();
      }
  }
于 2018-03-15T10:39:31.503 回答
0

感谢收看

  public boolean testPerfect(int n){
    int i=1;
    int sum=0;
    while(i<n){
        if(n%i==0)
        {
            sum+=i++;
        }
        else{
        i++;}
    }
    if (sum==n){
        return true;
    }
    return false;
}
于 2021-11-30T12:04:52.227 回答