1到1000之间的完美数

Perfect numbers between 1 and 1000

我不明白为什么这个程序没有给出任何输出。我只是看不到错误。这是一个程序,用于查找 1 到 1000 之间的所有完美数字。请帮我找出错误。谢谢

#include <stdio.h>

int main(){
    int number=1, i, sum=0;

    while(number<=1000){

        for(i=1; i<number; i++){
            if(number%i==0){
                sum+=i;
            }
        }

        if(sum==number){
            printf("%d is perfect\n", numero);
        }
        number++;
    }

    return 0;   
}

你没有在循环中重置 sum:

#include <stdio.h>

int main(){
    int number=1;

    while(number<=1000){
        int i, sum=0;

        for(i=1; i<number; i++){
            if(number%i==0){
                sum+=i;
            }
        }

        if(sum==number){
            printf("%d is perfect\n", numero);
        }
        number++;
    }

    return 0;

}

备案,据Wikipedia

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself) i.e. σ1(n) = 2n.

如前所述,您必须在循环开始时将 sum 重置为零。
您还必须将 numero 替换为 number(打字错误)。
为了更进一步,我建议您使用 for 循环而不是 while 循环,恕我直言,我发现 while 循环更具可读性。内部for循环也可以在i <= number/2时停止以优化一下。这是我的代码建议:

#include <stdio.h>

int main(){

    int sum;

    for(int number = 1;number <= 1000; number++){
        sum = 0;

        for (int i = 1; i <= number/2; i++){
            if (number % i == 0){
                sum += i;
            }
        }

        if (sum == number){
            printf("%d is perfect\n", number);
        }
    }

    return 0;
}

输出:

6 is perfect
28 is perfect
496 is perfect

1到1000之间有3个完全数。

{6, 28, 496}

您的代码大部分是正确的,但您忘记在每次循环迭代后重置总和值。

 ...
    }
    number++;
    sum = 0; // set sum to zero at the end of the while loop
  }

而且您在 printf 函数中将 number 变量拼错为 numero

printf("%d is perfect\n", numero);
#include <stdio.h>

int main(){

    int number = 1, sum = 0;

    while(number<=1000){

        sum = 0;
        for(int i = 1; i < number; i++){

            if(number % i == 0){
                sum += i;
            }
        }

        if(sum == number){
            printf("%d is perfect\n", number);
        }
        number++;
    }
    return 0;
}

将 sum 的值重置为 0 并将 numero 更改为 printf 中的数字

这段代码工作得很好,对其他答案中提出的所有建议稍加修改。

#include<stdio.h>

void main(){

int number = 1;

    while(number <= 1000){
    
       int i, sum = 0; 

       for(i=1; i<number; i++){

          if(number%i == 0){

              sum+=i;               
          }

          if( i == number-1 && sum == number){
             
               //modify the expression within the parenthesis
            
               printf("%d is a perfect number.\n", number);
            
               sum=0;
          }

      }

      number++;

  }

}

输出:

6 is a perfect number.
28 is a perfect number.
496 is a perfect number.

除了其他答案指出的其他错误外,还需要修改最后一个if语句括号内的表达式。这样做是为了防止包含此类数字,在这种情况下,在计算所有因数之前,因数之和等于该数。 如果保持不变,输出也应包括数字 24。