public static long Solve()
        {
            long sum       = 0;
            long sumFirst  = 0;
            long sumSecond = 0;

            for (int i = 1; i < 10000; i++)
            {
                sumFirst  = 0;
                sumSecond = 0;
                // we get the divisors of i and add them together
                long[] divisors = Toolbox.GetDivisors(i);
                for (int j = 0; j < divisors.Length - 1; j++)
                {
                    sumFirst += divisors[j];
                }
                // if the sum of divisors is not equal to the number itself we can continue
                if (sumFirst != i)
                {
                    // we get the divisors of the previous sum
                    divisors = Toolbox.GetDivisors(sumFirst);
                    for (int j = 0; j < divisors.Length - 1; j++)
                    {
                        sumSecond += divisors[j];
                    }
                    // if the second sum matches the original number, the number is considered to be amicable
                    if (sumSecond == i)
                    {
                        sum += sumFirst + i;
                    }
                }
            }
            // we divide the result by 2, because we added each number of the pair twice
            return(sum / 2);
        }
示例#2
0
        public static long Solve()
        {
            // first we need to find all the abundant numbers up to cca. 28123 - just to be safe
            long[]     divisors;
            long       sum      = 0;
            List <int> abundant = new List <int>();

            for (int i = 1; i < 28123; i++)
            {
                sum      = 0;
                divisors = Toolbox.GetDivisors(i);
                for (int j = 0; j < divisors.Length - 1; j++)
                {
                    sum += divisors[j];
                }
                if (sum > i)
                {
                    abundant.Add(i);
                }
            }

            // then we need to add each number in the set to each other to get all the possible numbers
            int        first, second, third;
            List <int> validNumbers = new List <int>();

            for (int i = 0; i < abundant.Count; i++)
            {
                first = abundant.ElementAt(i);
                for (int j = 0; j < abundant.Count; j++)
                {
                    second = abundant.ElementAt(j);
                    third  = first + second;
                    if (third < 28123)
                    {
                        validNumbers.Add(third);
                    }
                }
            }
            validNumbers = validNumbers.Distinct().ToList();

            sum = 0;
            // finally we need to check which numbers are not contained in the set and add them together
            for (int i = 1; i < 28123; i++)
            {
                if (!validNumbers.Contains(i))
                {
                    sum += i;
                }
            }

            return(sum);
        }
        public static long Solve()
        {
            long[] divisors = new long[] { };
            int    num      = 1;
            int    counter  = 2;

            while (divisors.Length <= 500)
            {
                num += counter;
                counter++;
                divisors = Toolbox.GetDivisors(num);
            }
            return(num);
        }