Updated for performance

This commit is contained in:
2020-06-17 12:33:25 -04:00
parent 6bb61ff9f3
commit a8fe06b3e4

View File

@@ -31,7 +31,7 @@ import java.util.Collections;
public class Problem21 extends Problem{
//The number that is > the largest number to be checked
private static final Integer LIMIT = 10000;
private static final int LIMIT = 10000;
public Problem21(){
super("Evaluate the sum of all the amicable numbers under 10000");
@@ -58,8 +58,8 @@ public class Problem21 extends Problem{
}
//Check every sum of divisors in the list for a matching sum
ArrayList<Integer> amicable = new ArrayList<Integer>();
for(Integer cnt = 1;cnt < divisorSum.size();++cnt){
Integer sum = divisorSum.get(cnt);
for(int cnt = 1;cnt < divisorSum.size();++cnt){
int sum = divisorSum.get(cnt);
//If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue
if(sum >= divisorSum.size()){
continue;
@@ -67,7 +67,7 @@ public class Problem21 extends Problem{
//We know that divisorSum.at(cnt) == sum, so if divisorSum.at(sum) == cnt we found an amicable number
if(divisorSum.get(sum).compareTo(cnt) == 0){
//A number can't be amicable with itself
if(sum.compareTo(cnt) == 0){
if(sum == cnt){
continue;
}
//Add it to the arraylist of amicable numbers
@@ -103,5 +103,5 @@ All amicable numbers less than 10000 are
6232
6368
The sum of all of these amicable numbers is 31626
It took 23.551 milliseconds to solve this problem.
It took 8.616 milliseconds to solve this problem.
*/