Updated for performance

This commit is contained in:
2020-06-16 13:36:09 -04:00
parent abd0ba9fbb
commit e266ccdf01

View File

@@ -1,7 +1,7 @@
//ProjectEuler/Java/Problem14.java //ProjectEuler/Java/Problem14.java
//Matthew Ellison //Matthew Ellison
// Created: 03-04-19 // Created: 03-04-19
//Modified: 03-28-19 //Modified: 06-16-20
/* /*
The following iterative sequence is defined for the set of positive integers: The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even) n → n/2 (n is even)
@@ -30,23 +30,23 @@ package mattrixwv.ProjectEuler.Problems;
public class Problem14 extends Problem{ public class Problem14 extends Problem{
//This is the top number that you will be checking against the series //This is the top number that you will be checking against the series
private static final Long MAX_NUM = 1000000L; private static final long MAX_NUM = 1000000L;
public Problem14(){ public Problem14(){
super("Which starting number, under one million, produces the longest chain using the itterative sequence?"); super("Which starting number, under one million, produces the longest chain using the itterative sequence?");
} }
public void solve(){ public void solve(){
//This is the length of the longest chain //This is the length of the longest chain
Long maxLength = 0L; long maxLength = 0L;
//This is teh starting number of the longest chain //This is the starting number of the longest chain
Long maxNum = 0L; long maxNum = 0L;
//Start the timer //Start the timer
timer.start(); timer.start();
//Loop through all numbers less than MAX_NUM and check them against the series //Loop through all numbers less than MAX_NUM and check them against the series
for(Long currentNum = 1L;currentNum < MAX_NUM;++currentNum){ for(long currentNum = 1L;currentNum < MAX_NUM;++currentNum){
Long currentLength = checkSeries(currentNum); long currentLength = checkSeries(currentNum);
//If the current number has a longer series than the max then the current becomes the max //If the current number has a longer series than the max then the current becomes the max
if(currentLength > maxLength){ if(currentLength > maxLength){
maxLength = currentLength; maxLength = currentLength;
@@ -61,8 +61,8 @@ public class Problem14 extends Problem{
result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength); result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
} }
//This function follows the rules of the sequence and returns its length //This function follows the rules of the sequence and returns its length
private Long checkSeries(Long num){ private long checkSeries(long num){
Long length = 1L; //Start at 1 becuase you need to count the starting number long length = 1L; //Start at 1 because you need to count the starting number
//Follow the series, adding 1 for each step you take //Follow the series, adding 1 for each step you take
while(num > 1){ while(num > 1){
@@ -82,5 +82,5 @@ public class Problem14 extends Problem{
/* Results: /* Results:
The number 837799 produced a chain of 525 steps The number 837799 produced a chain of 525 steps
It took 919.500 microseconds to solve this problem. It took 260.459 milliseconds to solve this problem.
*/ */