mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
103 lines
3.4 KiB
Java
103 lines
3.4 KiB
Java
//ProjectEuler/C++/Problem23.java
|
|
//Matthew Ellison
|
|
// Created: 03-22-19
|
|
//Modified: 03-28-19
|
|
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
|
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
|
/*
|
|
Copyright (C) 2019 Matthew Ellison
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
import mattrixwv.Stopwatch;
|
|
import mattrixwv.Algorithms;
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class Problem23{
|
|
public static final Integer MAX_NUM = 28123;
|
|
public static void main(String[] args){
|
|
Stopwatch timer = new Stopwatch();
|
|
//Setup the variables
|
|
ArrayList<Integer> divisorSums = new ArrayList<Integer>(); //Holds the sum of all the divisors of a number
|
|
divisorSums.ensureCapacity(MAX_NUM); //It is faster to reserve the appropriate amount of ram now
|
|
//Make sure every element has a 0 in it's location
|
|
for(int cnt = 0;cnt < MAX_NUM;++cnt){
|
|
divisorSums.add(0);
|
|
}
|
|
divisorSums.add(0);
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
//Get the sum of the divisors of all numbers < MAX_NUM
|
|
for(Integer cnt = 1;cnt.compareTo(MAX_NUM) < 0;++cnt){
|
|
ArrayList<Integer> div = Algorithms.getDivisors(cnt);
|
|
//Remove the last element, which is the number itself. This gives us the propper divisors
|
|
if(div.size() > 1){
|
|
div.remove(div.size() - 1);
|
|
}
|
|
divisorSums.set(cnt, Algorithms.getSum(div));
|
|
}
|
|
|
|
//Get the abundant numbers
|
|
ArrayList<Integer> abund = new ArrayList<Integer>();
|
|
for(Integer cnt = 0;cnt.compareTo(divisorSums.size()) < 0;++cnt){
|
|
if(divisorSums.get(cnt) > cnt.longValue()){
|
|
abund.add(cnt);
|
|
}
|
|
}
|
|
|
|
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
|
|
Long sum = 0L;
|
|
for(Integer cnt = 1;cnt.compareTo(MAX_NUM) < 0;++cnt){
|
|
if(!isSum(abund, cnt)){
|
|
sum += cnt.longValue();
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Print the results
|
|
System.out.printf("The answer is %d\n", sum);
|
|
System.out.printf("It took %s to run this algorithm\n", timer.getStr());
|
|
}
|
|
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
|
private static Boolean isSum(final ArrayList<Integer> abund, Integer num){
|
|
Integer sum = 0;
|
|
//Pick a number for the first part of the sum
|
|
for(Integer firstNum = 0;firstNum < abund.size();++firstNum){
|
|
//Pick a number for the second part of the sum
|
|
for(Integer secondNum = firstNum;secondNum < abund.size();++secondNum){
|
|
sum = abund.get(firstNum) + abund.get(secondNum);
|
|
if(sum.equals(num)){
|
|
return true;
|
|
}
|
|
else if(sum.compareTo(num) > 0){
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//If you have run through the entire list and did not find a sum then it is false
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* Results:
|
|
The answer is 4179871
|
|
It took 75.846 seconds to run this algorithm
|
|
*/
|