Please help with writing a loop for the program that must display/print every prefect number from 1 through 1000

4
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by lxXTaCoXxl

First you must tell the computer how to calculate a perfect number, if I remember correctly you're meaning prime which simply means it's only divisible by itself and 1. So create a list of integers then add each prime as it finds it. Hope it helps.

``````List<int> Primes = new List<int>();

for (double d1 = 0; d1 < 10000; d1++) {
if (ConditionMet)
}
``````

A perfect number is a number that is equal to the sum of its whole factors, excluding itself. Like 6 (1 + 2 + 3 = 6). So you need a function that calculates the sum of all factors then compare this with the number itself.

``````public static int SumOfFactors(int number)
{
return (from potentialFactor in Enumerable.Range(1, (int)Math.Sqrt(number))
where number % potentialFactor == 0
select potentialFactor).Sum();
}
``````

I will let you figuire out the rest.

Do you really mean perfect numbers?
If so, there are only three (3) between 1 and 1000.
As @lxXTaCoXxl says, you probably mean prime numbers

The formula you use for each will be completely different :)

Yes; and as @thines01 stated, if you are looking for perfect numbers then the formula would be completely different.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.