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.
Translated by translate.google.com:
Deixe d (n) ser definido como a soma dos divisores de n (números menores que n que dividem uniformemente em n).
Se d (a) = b e d (b) = a, onde a ≠ b, então a e b são um par amigável e cada um de a e b são chamados de números amigáveis.
Por exemplo, os divisores de 220 são 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 e 110, portanto d (220) = 284. Os divisores de 284 são 1, 2, 4, 71 e 142, assim d (284) = 220.
Avaliar a soma de todos os números amigáveis com 10 mil.
Meu código é o seguinte:
package Problem21;
import java.util.*;
public class AmicableN {
public static void main(String[] args) {
AmicableN a = new AmicableN();
List<Integer> aux = new LinkedList<Integer>();
for (int i = 1; i < 10000; i++) {
for (int j = 1; j < 10000; j++) {
if (i != j) {
if (!aux.contains(i + j)) {
if (a.isAmicable(i, j)) {
aux.add(i + j);
}
}
}
}
}
int ret = 0;
for (int i : aux)
ret += i;
System.out.println(ret);
}
public boolean isAmicable(int a, int b) {
int auxa = this.sumDivisors(a);
int auxb = this.sumDivisors(b);
if (auxa == b && auxb == a) {
return true;
}
return false;
}
public int sumDivisors(int a) {
int ret = 0;
for (int i = 1; i < a; ++i) {
if (a % i == 0) {
ret += i;
}
}
return ret;
}
}
Mas este meu código esta demorando demais para calcular, alguem teria uma solução mais trivial ?