寻找完全数2.c 459 B

123456789101112131415161718192021222324252627282930
  1. #include <stdio.h>
  2. int sum_of_divisors(int num) {
  3. int sum = 0;
  4. int i;
  5. for (i = 1; i < num; ++i) {
  6. if (num % i == 0) {
  7. sum += i;
  8. }
  9. }
  10. return sum;
  11. }
  12. int main(void) {
  13. int m;
  14. scanf("%d", &m);
  15. int max_perfect_num = 0;
  16. int i;
  17. for (i = 1; i <= m; ++i) {
  18. if (sum_of_divisors(i) == i) {
  19. max_perfect_num = i;
  20. }
  21. }
  22. printf("%d",max_perfect_num);
  23. return 0;
  24. }