Abundant numbers… those whose factors summed are greater than the number.
The challenge here is to find all abundant numbers up to 28123, then to find all those numbers which can’t be written as the sum of two abundant numbers.
So 12 is the first abundant number, whose factors add up to 16… making the smallest number with a sum of abundant numbers = 24.
http://projecteuler.net/index.php?section=problems&id=23
This works.. but fairly dumb code…. still .. enough to get me to the next challenge!
_______________
static void Main(string[] args)
{
List<int> abundantnumbers = new List<int>();
int sum=0; //non abundant numbers factors
for (int i=1;i<28123;i++)
{
if (i < sumofdivisors(divisors(i)))
{
abundantnumbers.Add(i);
}
//can i be found using all abundant numbers found so far
if (!issumofabundantnumners(abundantnumbers, i))
{
sum += i;
}
}
Console.WriteLine(sum);
Console.ReadLine();
}
static List<int> divisors(int n)
{
List<int> div = new List<int>();
for (int d = 1; d < n; d++)
{
if (n % d == 0) { div.Add(d); }
}
return div;
}
static int sumofdivisors(List<int> div)
{
int sum = 0;
foreach (int i in div)
{
sum += i;
}
return sum;
}
static bool issumofabundantnumners(List<int> ab, int i)
{
foreach (int i1 in ab)
{
foreach (int i2 in ab)
{
if (i == (i1 + i2))
{
return true;
}
}
}
return false;
}
No comments:
Post a Comment