mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Updated for performance
This commit is contained in:
@@ -413,28 +413,33 @@ public class Problem22 extends Problem{
|
|||||||
//Sort all the names
|
//Sort all the names
|
||||||
Collections.sort(names);
|
Collections.sort(names);
|
||||||
//Step through every name adding up the values of the characters
|
//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
|
//Step through every character in the current name adding up the value of the characters
|
||||||
sums.add(0L);
|
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
|
//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);
|
sums.set(nameCnt, sums.get(nameCnt) + names.get(nameCnt).charAt(charCnt) - 64);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Get the product for all numbers
|
//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));
|
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
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//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:
|
/* Results:
|
||||||
The answer to the question is 871198282
|
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.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user