简体   繁体   中英

Project Euler #21

Condition :

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

I did the following :

    static void Main()
    {
        long sum = 0;
        List<int> passedValues = new List<int>();
        for (int i = 1; i < 10000; i++)
        {
            var number1 = SumOfNumber(i);
            var number2 = SumOfNumber(SumOfNumber(i));
            if (number2 == i && !passedValues.Contains(number1))
            {
                sum = sum + number1;
                passedValues.Add(number1);
                passedValues.Add(number2);
            }
        }
        Console.WriteLine(sum);
        Console.ReadKey();
    }

    private static int SumOfNumber(int input)
    {
        int sum = 0;
        for (int i = 1; i <= input/2; i++)
        {
            if (input%i == 0)
            {
                sum += i;
            }
        }
        return sum;
    }

however it gives result 40284 while the correct answer seems to be 31626 why is my program not working properly ? Am I adding something multiple times ? I also tried adding a list to store the passed values, however it ended up giving a result 25008 :

static void Main()
    {
        long sum = 0;
        List<int> passed = new List<int>();
        for (int i = 1; i < 10000; i++)
        {
            var number1 = SumOfNumber(i);
            var number2 = SumOfNumber(SumOfNumber(i));
            if (number2 == i && !passed.Contains(i))
            {
                sum = sum + number1;
                passed.Add(number1);
            }
        }
        Console.WriteLine(sum);
        Console.ReadKey();
    }

There are two problems here:

  1. You are not adding both amicable pair numbers to the sum.
  2. You are including perfect numbers (where d(n) = n), which don't qualify as amicable pairs because a ≠ b is violated.

I think you were closer when you didn't add in the list to store passed numbers, because that caused issue #1 above since you are adding only the contribution of number1 to the sum, but adding both number1 and number2 to the list, causing number2 to eventually get skipped. To address issue #2, you also need to validate number1 != number2 . For example:

if (number2 == i && number1 != number2)
                 ^^^^^^^^^^^^^^^^^^^^^ add this check
{
    sum = sum + i;

After applying both of these fixes to your provided code, I am getting the expected total of 31626.

I got the result as 31626. The difference here is how to prevent duplicate in the sum. Instead of saving into a list, just to make sure i is always less than number1.

 static void Main()
    {

        long sum = 0;
        List<int> passedValues = new List<int>();
        for (int i = 1; i < 10000; i++)
        {
            var number1 = SumOfNumber(i);
            var number2 = SumOfNumber(SumOfNumber(i));


            if (number2 == i && i < number1)
            {
                sum = sum + i + number1;

            }
        }
        Console.WriteLine(sum );
        Console.ReadKey();
    }

    private static int SumOfNumber(int input)
    {
        int sum = 0;
        for (int i = 1; i <= input / 2; i++)
        {
            if (input % i == 0)
            {
                sum += i;
            }
        }
        return sum;
    }
private static int SumOfNumber(int input)
{
    int sum = 0;
    for (int i = 1; i <= input/2; i++)
    {
        if (input%i == 0)
        {
            sum += i;
        }
    }
    return sum;
}

This is not correct. You are only adding one of the factors, and not looping up to the sqrt of the number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM