Updated for performance

This commit is contained in:
2020-06-15 19:39:58 -04:00
parent efe2d5a07a
commit d570274fa9

View File

@@ -55,14 +55,14 @@ public class Problem8 extends Problem{
public void solve(){
//Setup the variables
String maxNums = new String(); //Holds the string of the largest product
Long maxProduct = 0L; //Holds the largest product of 13 numbers
long maxProduct = 0L; //Holds the largest product of 13 numbers
//Start the timer
timer.start();
//Cycle through the string of numbers looking for the maximum product
for(Integer cnt = 12;cnt < NUMBER.length();++cnt){
Long currentProduct = Long.parseLong(NUMBER.substring(cnt - 12, cnt - 11)) * Long.parseLong(NUMBER.substring(cnt - 11, cnt - 10)) * Long.parseLong(NUMBER.substring(cnt - 10, cnt - 9)) * Long.parseLong(NUMBER.substring(cnt - 9, cnt - 8)) * Long.parseLong(NUMBER.substring(cnt - 8, cnt - 7)) * Long.parseLong(NUMBER.substring(cnt - 7, cnt - 6)) * Long.parseLong(NUMBER.substring(cnt - 6, cnt - 5)) * Long.parseLong(NUMBER.substring(cnt - 5, cnt - 4)) * Long.parseLong(NUMBER.substring(cnt - 4, cnt - 3)) * Long.parseLong(NUMBER.substring(cnt - 3, cnt - 2)) * Long.parseLong(NUMBER.substring(cnt - 2, cnt - 1)) * Long.parseLong(NUMBER.substring(cnt - 1, cnt)) * Long.parseLong(NUMBER.substring(cnt, cnt + 1));
for(int cnt = 12;cnt < NUMBER.length();++cnt){
long currentProduct = Long.parseLong(NUMBER.substring(cnt - 12, cnt - 11)) * Long.parseLong(NUMBER.substring(cnt - 11, cnt - 10)) * Long.parseLong(NUMBER.substring(cnt - 10, cnt - 9)) * Long.parseLong(NUMBER.substring(cnt - 9, cnt - 8)) * Long.parseLong(NUMBER.substring(cnt - 8, cnt - 7)) * Long.parseLong(NUMBER.substring(cnt - 7, cnt - 6)) * Long.parseLong(NUMBER.substring(cnt - 6, cnt - 5)) * Long.parseLong(NUMBER.substring(cnt - 5, cnt - 4)) * Long.parseLong(NUMBER.substring(cnt - 4, cnt - 3)) * Long.parseLong(NUMBER.substring(cnt - 3, cnt - 2)) * Long.parseLong(NUMBER.substring(cnt - 2, cnt - 1)) * Long.parseLong(NUMBER.substring(cnt - 1, cnt)) * Long.parseLong(NUMBER.substring(cnt, cnt + 1));
//Check if the product is greater than the current maximum
if(currentProduct > maxProduct){
@@ -75,12 +75,12 @@ public class Problem8 extends Problem{
timer.stop();
//Save the results
result = String.format("The greatest product is " + maxProduct.toString() + "\nThe numbers are " + maxNums);
result = String.format("The greatest product is " + maxProduct + "\nThe numbers are " + maxNums);
}
}
/* Results:
The greatest product is 23514624000
The numbers are 5576689664895
It took 2.901 milliseconds to solve this problem.
It took 806.200 microseconds to solve this problem.
*/