Project Euler: Problem 21 – Amicable numbers

Problem 21:

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.

Idea:

Find the proper divisors of a given number and add them up. Then repeat for the sum and add up those proper divisors and see if the two numbers form a cycle, and are not the same number.

int answer = 0;

int[] values = new int[10001];
for(int i = 0; i < values.length; i++) {
   int sum = 1; //all will have minimum value of 1
   for(int divisor = 2; divisor <= Math.sqrt(i); divisor++) {
      int quotient = i/divisor;
      if(quotient * divisor == i) {
         if(divisor != quotient) {
            sum+=divisor;
            sum+=quotient;
         } else {
            sum+=divisor;
         }
      }
   }
   values[i] = sum;
}

for(int i = 1; i < values.length; i++) {
   //Sample inputs to distinguish between good and bad
   //     i = 6   220
   // first = 6   284
   //second = 6   220
   //        bad  good
   int first = values[i];
   if(first < values.length && first != 1) {
      int second = values[first];
      if(second == i && first != i) {
         answer+=i;
      }
   }
}

System.out.println("Answer: "+answer);