1

I have the following program, the perfect numbers won't print, only output are the numbers 1 and 2, which are not perfect. What's wrong, is it a scope problem or is it the loops? Adding break statement after print statement causes output of all numbers 1 - 99.

int sum = 0;

for (int i = 1; i < 100; i++) {

    for (int j = 1; j <= i; j++) {

        if (i % j == 0){

            sum += j;}

        if (sum == i){

            printf("%d\n", i);

        }
    }
}
Waffl
  • 35
  • 6
  • Well, neither 1 or 2 are perfect numbers. So I'd start with that. You should look at [this](http://stackoverflow.com/questions/26753839/efficiently-getting-all-divisors-of-a-given-number) and work from there. – RamblingMad Jul 27 '15 at 23:13
  • 1
    Sum should restart at zero every iteration of the inner loop. – o11c Jul 27 '15 at 23:17
  • Note that there are only two perfect numbers below 100 (they are not 1 and 2, that's due to the error in your loop condition), and only five below 100 million, so you won't find a lot of them any time soon with this method, hopefully this is just an exercise. =) – Arkku Jul 27 '15 at 23:26
  • 1
    when asking a question about a runtime problem, please post code that cleanly compiles (the post code doesn't even begin to compile.) Then (for this case) post the expected output and the actual output. And since the topic is 'perfect numbers' post the definition of a perfect number. – user3629249 Jul 27 '15 at 23:30

3 Answers3

4

Three problems:

  1. sum must be initialized to zero for every i, not just in the beginning
  2. the second loop condition must be j < i, as per definition of perfect number the number itself is excluded
  3. the check for sum == i and following printf must be moved outside the inner loop, otherwise it prints intermediate results
melpomene
  • 84,125
  • 8
  • 85
  • 148
Arkku
  • 41,011
  • 10
  • 62
  • 84
1

given this code, which incorporates all the prior comments/answers:

#include <stdio.h>


int main( void )
{
    int sum = 0;

    for (int i = 1; i < 100; i++)
    {
        sum = 0; // reset on each new number

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

        if (sum == i)
        {
             printf("%d\n", i);
        } // end if
    } // end for
    return 0;
} // end function: main

the output is 6 and 28

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

I think you don't need to test all the way to i (j < i)... it is enough to reach i / 2 ...

#include<stdio.h>
void main() {
    int sum;
    for (int i = 1; i < 100; i++) {
        sum = 0;
        for (int j = 1; j <= i/2; j++) {
            if (i % j == 0) {
                sum += j;
            }
        }
        if (sum == i) {
            printf("%d\n", i);
        }
    }
}
mlwn
  • 1,156
  • 1
  • 10
  • 25
  • 1
    If we want to get into optimizations, there are a lot of other things to change as well, but in the end the fastest code to print all perfect numbers within the range of a 32-bit `int` is `puts("6\n28\n496\n8128\n33550336");` =) – Arkku Jul 27 '15 at 23:31
  • 1
    @Arkku and I can think of even faster code to print all the odd ones, even using unsigned long long – John Coleman Jul 27 '15 at 23:34