mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Updated for performance
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEuler/Java/Problem28.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 09-22-19
|
// 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
|
//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
|
//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
|
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
|
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
|
//Holds the grid that we will be filling and searching
|
||||||
private static ArrayList<ArrayList<Integer>> grid;
|
private static ArrayList<ArrayList<Integer>> grid;
|
||||||
//Holds the sum of the diagonals of the grid
|
//Holds the sum of the diagonals of the grid
|
||||||
private Integer sumOfDiagonals = 0;
|
private int sumOfDiagonals = 0;
|
||||||
|
|
||||||
public Problem28(){
|
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");
|
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(){
|
private void setupGrid(){
|
||||||
grid = new ArrayList<ArrayList<Integer>>();
|
grid = new ArrayList<ArrayList<Integer>>();
|
||||||
//Fill the grid with 0's
|
//Fill the grid with 0's
|
||||||
for(Integer cnt = 0;cnt < 1001;++cnt){
|
for(int cnt = 0;cnt < 1001;++cnt){
|
||||||
//Add a blank ArrayList
|
//Add a blank ArrayList
|
||||||
grid.add(new ArrayList<Integer>());
|
grid.add(new ArrayList<Integer>());
|
||||||
for(Integer cnt2 = 0;cnt2 < 1001;++cnt2){
|
for(int cnt2 = 0;cnt2 < 1001;++cnt2){
|
||||||
grid.get(cnt).add(0);
|
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
|
//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
|
//Start with the middle location and set it correctly and advance the tracker to the next number
|
||||||
Integer xLocation = 500;
|
int xLocation = 500;
|
||||||
Integer yLocation = 500;
|
int yLocation = 500;
|
||||||
grid.get(yLocation).set(xLocation, currentNum++);
|
grid.get(yLocation).set(xLocation, currentNum++);
|
||||||
//Move right the first time
|
//Move right the first time
|
||||||
++xLocation;
|
++xLocation;
|
||||||
@@ -81,22 +81,22 @@ public class Problem28 extends Problem{
|
|||||||
grid.get(yLocation).set(xLocation, currentNum++);
|
grid.get(yLocation).set(xLocation, currentNum++);
|
||||||
++xLocation;
|
++xLocation;
|
||||||
//Check if you are at the final location and break the loop if you are
|
//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;
|
finalLocation = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Finds the sum of teh diagonals in the grid
|
//Finds the sum of the diagonals in the grid
|
||||||
private void findSum(){
|
private void findSum(){
|
||||||
//Start at teh top corners and work your way down moving toward the opposite side
|
//Start at the top corners and work your way down moving toward the opposite side
|
||||||
Integer leftSide = 0;
|
int leftSide = 0;
|
||||||
Integer rightSide = grid.size() - 1;
|
int rightSide = grid.size() - 1;
|
||||||
Integer row = 0;
|
int row = 0;
|
||||||
while(row < grid.size()){
|
while(row < grid.size()){
|
||||||
//This ensures the middle location is only counted once
|
//This ensures the middle location is only counted once
|
||||||
if(Integer.compare(leftSide, rightSide) == 0){
|
if(leftSide == rightSide){
|
||||||
sumOfDiagonals += grid.get(row).get(leftSide);
|
sumOfDiagonals += grid.get(row).get(leftSide);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@@ -127,5 +127,5 @@ public class Problem28 extends Problem{
|
|||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of the diagonals in the given grid is 669171001
|
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.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user