I'm trying to solve problem 21 from Project Euler site, I think that I don't have any mistakes, but I get wrong result, can anyone find mistake that occurs. I get 18320, but correct answer is 31626.
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.
But in task text, 220 don't have 220 as divisor. So, I suppose that we don't need to include number as divisor.
And I have tried that, but I get result 2.
> But in task text, 220 don't have 220 as divisor
Then just use your old function.
1 2 3 4 5 6
unsignedint n = 10000;
unsignedlonglong sum = 0;
for(unsignedint i=1; i<n; i++)
if(sumOfDivisors(i)==sumOfDivisors(sumOfDivisors(i)))
sum +=(sumOfDivisors(i)+sumOfDivisors(sumOfDivisors(i)));
cout << sum << endl;
Your program is actually doing too much. According to your algorithm, only a single function call is more than enough to fix the problem.
1 2 3
unsignedint n = 10000;
unsignedlonglong sum = sumOfDivisors(n);
cout << sum << endl;
Yes, I have that solution, but I want to know why my solution don't work, I don't want to just copy-paste solution from internet
And also, I have tried your solution, but it don't work too.