mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-07 01:23:56 -05:00
Updated for performance
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEuler/C++/Problem23.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-22-19
|
// Created: 03-22-19
|
||||||
//Modified: 03-28-19
|
//Modified: 06-17-20
|
||||||
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2019 Matthew Ellison
|
Copyright (C) 2020 Matthew Ellison
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -30,7 +30,7 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class Problem23 extends Problem{
|
public class Problem23 extends Problem{
|
||||||
//The largest number to be checked
|
//The largest number to be checked
|
||||||
public static final Integer MAX_NUM = 28123;
|
public static final int MAX_NUM = 28123;
|
||||||
|
|
||||||
public Problem23(){
|
public Problem23(){
|
||||||
super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers");
|
super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers");
|
||||||
@@ -47,8 +47,9 @@ public class Problem23 extends Problem{
|
|||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Get the sum of the divisors of all numbers < MAX_NUM
|
//Get the sum of the divisors of all numbers < MAX_NUM
|
||||||
for(Integer cnt = 1;cnt.compareTo(MAX_NUM) < 0;++cnt){
|
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
||||||
ArrayList<Integer> div = Algorithms.getDivisors(cnt);
|
ArrayList<Integer> div = Algorithms.getDivisors(cnt);
|
||||||
//Remove the last element, which is the number itself. This gives us the propper divisors
|
//Remove the last element, which is the number itself. This gives us the propper divisors
|
||||||
if(div.size() > 1){
|
if(div.size() > 1){
|
||||||
@@ -59,17 +60,17 @@ public class Problem23 extends Problem{
|
|||||||
|
|
||||||
//Get the abundant numbers
|
//Get the abundant numbers
|
||||||
ArrayList<Integer> abund = new ArrayList<Integer>();
|
ArrayList<Integer> abund = new ArrayList<Integer>();
|
||||||
for(Integer cnt = 0;cnt.compareTo(divisorSums.size()) < 0;++cnt){
|
for(int cnt = 0;cnt < divisorSums.size();++cnt){
|
||||||
if(divisorSums.get(cnt) > cnt.longValue()){
|
if(divisorSums.get(cnt) > cnt){
|
||||||
abund.add(cnt);
|
abund.add(cnt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
|
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
|
||||||
Long sum = 0L;
|
long sum = 0L;
|
||||||
for(Integer cnt = 1;cnt.compareTo(MAX_NUM) < 0;++cnt){
|
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
||||||
if(!isSum(abund, cnt)){
|
if(!isSum(abund, cnt)){
|
||||||
sum += cnt.longValue();
|
sum += cnt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,17 +81,17 @@ public class Problem23 extends Problem{
|
|||||||
result = String.format("The answer is %d\n", sum);
|
result = String.format("The answer is %d\n", sum);
|
||||||
}
|
}
|
||||||
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
||||||
private Boolean isSum(final ArrayList<Integer> abund, Integer num){
|
private boolean isSum(final ArrayList<Integer> abund, int num){
|
||||||
Integer sum = 0;
|
int sum = 0;
|
||||||
//Pick a number for the first part of the sum
|
//Pick a number for the first part of the sum
|
||||||
for(Integer firstNum = 0;firstNum < abund.size();++firstNum){
|
for(int firstNum = 0;firstNum < abund.size();++firstNum){
|
||||||
//Pick a number for the second part of the sum
|
//Pick a number for the second part of the sum
|
||||||
for(Integer secondNum = firstNum;secondNum < abund.size();++secondNum){
|
for(int secondNum = firstNum;secondNum < abund.size();++secondNum){
|
||||||
sum = abund.get(firstNum) + abund.get(secondNum);
|
sum = abund.get(firstNum) + abund.get(secondNum);
|
||||||
if(sum.equals(num)){
|
if(sum == num){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if(sum.compareTo(num) > 0){
|
else if(sum > num){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,5 +103,5 @@ public class Problem23 extends Problem{
|
|||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The answer is 4179871
|
The answer is 4179871
|
||||||
It took 49.791 seconds to solve this problem.
|
It took 12.277 seconds to solve this problem.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user