mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-07 09:23:57 -05:00
Updated for performance
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user