Problem 21. Amicable numbers

Description

public class euler21 {
    public static void main(String[]args) {
        int[] array = new int[10000];
       	int sum = 0;
      	for (int i = 1; i < 10000; i++) {
            int s = sumDiv(i);
            if (s < 10000)
              	array[i] = sumDiv(i);
      	}
        for (int i = 0; i < 10000; i++) {
            int j = array[i];
            if (i == array[j] && i != array[i]) {
                sum += i;
            }
        }
       	System.out.println(sum);
    }
    public static int sumDiv(int n) {
       	int sum = 0;
       	for (int i = 1; i*i < n; i++) {
            if (n % i == 0) {
              	if (i > 1)
                   sum += n/i;
             	sum += i;
       	    }
      	}
      	return sum;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>