Jason Rowe

Be curious! Choose your own adventure.

Interview Programming Question – find perfect numbers

Here is a problem I got once for a entry level programming job. I received lots of help from the person asking and it turned out to be a good experience. If you ever use this in an interview be nice because this really has little to do with real world programming and more about finding out about the person.

Question: Create a method to find perfect numbers. In mathematics, a perfect number is defined as a positive integer which is the sum of its proper positive divisors

Possible Answer:


class Program
{
static void Main()
{

for (int j = 1; j < 10000; j++)
{
if (j % 2 == 0)
{
if (PerfectNumber.IsPerfectNumber(j))
Console.WriteLine(j + "is perfect");
}
}
Console.ReadLine();
}
}

public static class PerfectNumber
{
public static bool IsPerfectNumber(int n)
{
var totalFactor = 0;
for (var i = 1; i < n; i++)
{
if(n % i == 0)
{
totalFactor = totalFactor + i;
}
}

return totalFactor == n;
}
}


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *