I am trying to accomplish a task. To print 4 perfect numbers which are between 1 and 10000.
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.
Here's my code:
public class PerfectNumbers
{
public static void main(String[] args)
{
// Perfect numbers!
for (int number = 1; number < 10000; number++)
{
int sum = 0;
int i = 1;
while (i < number)
{
if (number % i == 0)
{
sum += i;
i++;
}
else
{
i++;
continue;
}
if (sum == number)
{
System.out.println(number);
}
else
{
continue;
}
}
}
}
}
The output is:
6
24 <--- This one is wrong because next must be 28.
28
496
2016
8128
8190
What is wrong in my code? Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…