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

Recommended Answers

All 4 Replies

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)
        Primes.Add(d1);
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.