Search code examples
javanumberssumperfect-numbers

Project Euler 23: Answer is off by 995


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 n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

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.

import java.util.*;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;


public class helloworld {

public static int[]array = new int[28124];
public static List<Integer> abundant = new ArrayList<Integer>();

public static void main(String []args)
    throws IOException {


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

 }
public static int SumNonAbundant() {
    int sum = 0;
    abundant.add(12);
    GetAbundant(28123);
    for (int i = 1; i <= 28123; i++) {

        if (checkForSum(i)) {
            System.out.println(i);
            sum+=i;

        }
    }
    return sum;
}

public static int SumOfDivisors(int num) {
    int sum = 0;
    for (int i = num - 1; i > 0; i--) {
        if (num % i == 0) {
            sum += i;

        }
    }
    return sum;
}

public static void GetAbundant(int num) {

    for (int i = 13; i <= num ; i++) {
        int sum = SumOfDivisors(i);
        if ( sum > i) {
            System.out.println(i + " " + sum);

            abundant.add(i);

        }
    }

}

public static boolean checkForSum(int num) {
    int start = 0;
    int end = abundant.size() - 1;


    while (start < end) {
        if (abundant.get(start) == num) {
            return false;
        }
        else if (abundant.get(end) == num) {
            return false;
        }
        else if (abundant.get(start)*2 == num) {
            return false;
        }
        else if (abundant.get(end)*2 == num) {
            return false;
        }
        else if (abundant.get(start) + abundant.get(end) == num) {
            return false;
        }
        else if (abundant.get(start) + abundant.get(end) < num) {
            start++;
        }
        else if (abundant.get(start) + abundant.get(end) > num) {
            end--;
        }
    }
    return true;
}



}

When I run this code, I get "Answer: 4178876", however, the correct answer I think is 4178971. Really not sure whats the issue here, feel like I'm missing something small but I can't see it. Any help would be greatly appreciated.


Solution

  • in checkForSum change

    if (abundant.get(start) == num) {
          return true;  // not false
    }
    

    and remove

    else if (abundant.get(end) == num) {
     ...
    }
    

    try

    public static boolean checkForSum(int num) {
        int start = 0;
        int end = abundant.size() - 1;
    
        while (start < end) {
            if (abundant.get(start) == num) {
                return true;
            }
            else if (abundant.get(start)*2 == num) {
                return false;
            }
            else if (abundant.get(end)*2 == num) {
                return false;
            }
            else if (abundant.get(start) + abundant.get(end) == num) {
                return false;
            }
            else if (abundant.get(start) + abundant.get(end) < num) {
                start++;
            }
            else if (abundant.get(start) + abundant.get(end) > num) {
                end--;
            }
        }
        return true;
    }