Updated for performance

This commit is contained in:
2020-06-17 11:22:22 -04:00
parent 43c0df0d22
commit 45ee548615

View File

@@ -37,24 +37,24 @@ package mattrixwv.ProjectEuler.Problems;
public class Problem19 extends Problem{
private static enum DAYS{SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, NUMBER_OF_DAYS, ERROR};
//The first year we are going to test
private static final Integer START_YEAR = 1901;
private static final int START_YEAR = 1901;
//The last year we are going to test
private static final Integer END_YEAR = 2000;
private static final int END_YEAR = 2000;
public Problem19(){
super("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?");
}
public void solve(){
//The total number of sundays
Long totalSundays = 0L;
long totalSundays = 0L;
//Start the timer
timer.start();
//Run for all years 1901-2000
for(Integer year = START_YEAR;year <= END_YEAR;++year){
for(int year = START_YEAR;year <= END_YEAR;++year){
//Run for all months in the year
for(Integer month = 1;month <= 12;++month){
for(int month = 1;month <= 12;++month){
DAYS day = getDay(month, 1, year);
if(day == DAYS.ERROR){
System.out.println("There was an error with the day");
@@ -72,18 +72,18 @@ public class Problem19 extends Problem{
result = String.format("There are %d Sundays that landed on the first of the months from %d to %d\n", totalSundays, START_YEAR, END_YEAR);
}
//Return the day of the week that the date you pass into it is on
private DAYS getDay(Integer month, Integer day, Integer year){
private DAYS getDay(int month, int day, int year){
//Make sure the numbers are within propper bounds
if((month < 1) || (month > 12) || (day < 1) || (day > 31) || (year < 1)){
return DAYS.ERROR;
}
Integer numDays = 0;
Integer currentYear = 1;
Integer currentMonth = 1;
Integer currentDay = DAYS.SATURDAY.ordinal();
int numDays = 0;
int currentYear = 1;
int currentMonth = 1;
int currentDay = DAYS.SATURDAY.ordinal();
--day;
//Add teh currect number of days for every year
//Add the correct number of days for every year
while(currentYear < year){
if(isLeapYear(currentYear)){
numDays += 366;
@@ -150,7 +150,7 @@ public class Problem19 extends Problem{
}
}
//Returns true if the year passed to it is a leap year
private Boolean isLeapYear(Integer year){
private boolean isLeapYear(int year){
if(year < 1){
return false;
}
@@ -172,5 +172,5 @@ public class Problem19 extends Problem{
/* Results:
There are 171 Sundays that landed on the first of the months from 1901 to 2000
It took 46.394 milliseconds to solve this problem.
It took 1.767 milliseconds to solve this problem.
*/