[英]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).
令d(n)定义为n的适当除数之和(小于n的数,均分为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.
如果d(a)= b且d(b)= a,其中a≠b,则a和b是友好对,而a和b的每一个都称为友好数。
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110;
例如,适当的220除数是1,2,4,5,10,11,20,22,44,55和110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142;
因此d(220)=284。284的适当除数是1,2,4,71和142; so d(284) = 220.
因此d(284)= 220。
Evaluate the sum of all the amicable numbers under 10000.
评估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 ? 但是它给出的结果是40284,而正确的答案似乎是31626,为什么我的程序不能正常工作? 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 :
我还尝试添加一个列表来存储传递的值,但是最终结果为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: 这里有两个问题:
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. 我认为当您不添加列表来存储传递的数字时,您就更近了,因为这导致了上面的问题#1,因为您只将
number1
的贡献添加到总和中,但是将number1
和number2
都添加到了列表中number2
最终被跳过。 To address issue #2, you also need to validate number1 != number2
. 要解决问题2,您还需要验证
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. 将这两个修复程序应用于您提供的代码后,我得到的预期总数为31626。
I got the result as 31626. The difference here is how to prevent duplicate in the sum. 我得到的结果是31626。这里的区别是如何防止总和重复。 Instead of saving into a list, just to make sure i is always less than number1.
只是确保我始终小于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.
您只添加了其中一个因素,而没有增加数字的平方根。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.