I want to find the first 5 perfect numbers.
This is my code, but there is a problem. My logic is that I stay inside the loop until i have the fifth number (i.e. test == 4) but it simply hangs. If I change the condition from (test<4) to (test<3) I get the first 4 perfect numbers without a problem though... I start from number = 4 because 2 and 3 are prime.
Code:
#include <stdio.h>
int main (int argc, const char * argv[])
{
long perfect[5];
long test = -1, i;
long number = 4, sum;
while (test < 4)
{
sum = 0;
for (i=1; i< number; i++)
{
if (broj % i == 0) sum += i;
}
if (sum == number)
{
test++;
perfect[test] = number;
}
number ++;
}
printf("::::: THE PERFECT NUMBERS ARE : ");
for (i=0; i<5; i++)
{printf("\t%d", perfect[i]);}
return 0;
}