0

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
5 Years
Discussion Span
Last Post by lxXTaCoXxl
0

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);
}
0

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.

0

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.