From 8acb129f956d20fe1977d304d9b824e70d2607ce Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 17 Jun 2020 14:08:08 -0400 Subject: [PATCH] Updated for performance --- .../ProjectEuler/Problems/Problem22.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java index 2eb0dc3..83e95d7 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java @@ -413,28 +413,33 @@ public class Problem22 extends Problem{ //Sort all the names Collections.sort(names); //Step through every name adding up the values of the characters - for(Integer nameCnt = 0;nameCnt < names.size();++nameCnt){ + for(int nameCnt = 0;nameCnt < names.size();++nameCnt){ //Step through every character in the current name adding up the value of the characters sums.add(0L); - for(Integer charCnt = 0;charCnt < names.get(nameCnt).length();++charCnt){ + for(int charCnt = 0;charCnt < names.get(nameCnt).length();++charCnt){ //A = 65 so subtracting 64 means A - 1. This will only work correctly if all letters are capitalized sums.set(nameCnt, sums.get(nameCnt) + names.get(nameCnt).charAt(charCnt) - 64); } } //Get the product for all numbers - for(Integer cnt = 0;cnt < sums.size();++cnt){ + for(int cnt = 0;cnt < sums.size();++cnt){ prod.add(sums.get(cnt) * (cnt + 1)); } + //Get the sum of all the numbers + long sum = Algorithms.getLongSum(prod); + System.out.println("sums last = " + sums.get(sums.size() - 4) + ", " + sums.get(sums.size() - 3) + ", " + sums.get(sums.size() - 2) + ", " + sums.get(sums.size() - 1)); + System.out.println("prod last = " + prod.get(prod.size() - 4) + ", " + prod.get(prod.size() - 3) + ", " + prod.get(prod.size() - 2) + ", " + prod.get(prod.size() - 1)); + //Stop the timer timer.stop(); //Save the results - result = String.format("The answer to the question is %d\n", Algorithms.getLongSum(prod)); + result = String.format("The answer to the question is %d\n", sum); } } /* Results: The answer to the question is 871198282 -It took 17.822 milliseconds to solve this problem. +It took 935.700 microseconds to solve this problem. */