Updated for performance

This commit is contained in:
2020-06-17 22:29:03 -04:00
parent f664a076a6
commit 64f43e0a4d

View File

@@ -1,11 +1,11 @@
//ProjectEuler/Java/Problem28.java
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java
//Matthew Ellison
// Created: 09-22-19
//Modified: 09-22-19
//Modified: 06-17-20
//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2019 Matthew Ellison
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -30,7 +30,7 @@ public class Problem28 extends Problem{
//Holds the grid that we will be filling and searching
private static ArrayList<ArrayList<Integer>> grid;
//Holds the sum of the diagonals of the grid
private Integer sumOfDiagonals = 0;
private int sumOfDiagonals = 0;
public Problem28(){
super("What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral");
@@ -39,20 +39,20 @@ public class Problem28 extends Problem{
private void setupGrid(){
grid = new ArrayList<ArrayList<Integer>>();
//Fill the grid with 0's
for(Integer cnt = 0;cnt < 1001;++cnt){
for(int cnt = 0;cnt < 1001;++cnt){
//Add a blank ArrayList
grid.add(new ArrayList<Integer>());
for(Integer cnt2 = 0;cnt2 < 1001;++cnt2){
for(int cnt2 = 0;cnt2 < 1001;++cnt2){
grid.get(cnt).add(0);
}
}
Boolean finalLocation = false; //A flag to indicate if the final location to be filled has been reached
boolean finalLocation = false; //A flag to indicate if the final location to be filled has been reached
//Set the number that is going to be put at each location
Integer currentNum = 1;
int currentNum = 1;
//Start with the middle location and set it correctly and advance the tracker to the next number
Integer xLocation = 500;
Integer yLocation = 500;
int xLocation = 500;
int yLocation = 500;
grid.get(yLocation).set(xLocation, currentNum++);
//Move right the first time
++xLocation;
@@ -81,22 +81,22 @@ public class Problem28 extends Problem{
grid.get(yLocation).set(xLocation, currentNum++);
++xLocation;
//Check if you are at the final location and break the loop if you are
if(xLocation.equals(grid.size())){
if(xLocation == grid.size()){
finalLocation = true;
break;
}
}
}
}
//Finds the sum of teh diagonals in the grid
//Finds the sum of the diagonals in the grid
private void findSum(){
//Start at teh top corners and work your way down moving toward the opposite side
Integer leftSide = 0;
Integer rightSide = grid.size() - 1;
Integer row = 0;
//Start at the top corners and work your way down moving toward the opposite side
int leftSide = 0;
int rightSide = grid.size() - 1;
int row = 0;
while(row < grid.size()){
//This ensures the middle location is only counted once
if(Integer.compare(leftSide, rightSide) == 0){
if(leftSide == rightSide){
sumOfDiagonals += grid.get(row).get(leftSide);
}
else{
@@ -127,5 +127,5 @@ public class Problem28 extends Problem{
/* Results:
The sum of the diagonals in the given grid is 669171001
It took 84.535 milliseconds to solve this problem.
It took 16.687 milliseconds to solve this problem.
*/