From d570274fa932ed47714a6241eb2d9f990557ec6b Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 15 Jun 2020 19:39:58 -0400 Subject: [PATCH] Updated for performance --- .../java/mattrixwv/ProjectEuler/Problems/Problem8.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java index ca641db..4f3ab80 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java @@ -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. */