A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number

nis called deficient if the sum of its proper divisors is less thannand it is called abundant if this sum exceedsn.As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

<!–

A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant.

–>

This is a two parter. You need a good factorization routine (previous problem) and using a modified Sieve of Eratosthenes technique, you can zero-out no applicable numebrs. Then just add them up. Algorithm takes about 3 seconds to run.

import java.util.*;

public class Euler23

{

public static void main(String[] args)

{

System.out.print(“Problem 23:\n”);

Euler23 e = new Euler23();

System.out.print(“Answer = ” + e.Problem()+ “\n”);

}

public String Problem ()

{

int [] N = new int[28124];

for (int i = 1; i <= 28123; i++)

{

N[i] = i;

}

ArrayList<Integer> A = new ArrayList<Integer>();

for (int i = 12; i <= 28123; i++)

{

int t = 0;

for (int n: F(i))

t += n;

if (t > i)

A.add(i);

}

for (int m : A)

for (int n: A)

if (m + n <= 28123)

N[m + n] = 0;

long sum = 0;

for (int i = 1; i <= 28123; i++)

{

sum += N[i];

}

return String.valueOf(sum);

}public ArrayList<Integer> F (int num)

{

ArrayList<Integer> factors = new ArrayList<Integer>();

factors.add(1);

for (int n = 2; n <= Math.ceil(Math.sqrt(num)); n++)

if (num % n == 0 && factors.contains(n) == false)

{

factors.add(n);

if (factors.contains(num/n) == false)

factors.add(num/n);

}

return factors;

}

}