Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

Tags:

java

I am new to programming and java and I am trying to solve the following problem: How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

Here is my code:

int count, sum = 0;           
for (int i = 1901; i < 2001; i++) {
    LocalDate test = LocalDate.of(i, 1, 1);
    sum += test.lengthOfYear();
}
for (int i = 1; i < sum; i++) {
    LocalDate date1 = LocalDate.of(1901, 1, 1);
    date1 = date1.plusDays(i);
    if (date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY) {
        count++;
    }
}
System.out.println(count);

If I print the results, it seems to be working fine.

My result is 443, but the correct answer is 171. What am I doing wrong?

Thank you!

like image 622
GeoTeo Avatar asked Jun 21 '17 15:06

GeoTeo


3 Answers

I suspect 443 is the total number of Sundays in January in the twentieth century. This happens because you walk over all possible days of the twentieth century and then check if the current month is January and if the current day is Sunday.

This is not what you want.

I would use a different approach:

  • Walk over the 1st day of each month of each year.
  • And then check if it's a Sunday.

The code will probably be much faster.

// Each year
for (int y = 1901; y < 2001; y++) {
    // Each month of the year
    for (int m = 1; m <= 12; m++) {
        if (LocalDate.of(y, m, 1).getDayOfWeek() == DayOfWeek.SUNDAY) {
            count++;
        }
    }
}

Your code would have been correct if you changed date1.getMonth() == JANUARY to date1.getDayOfMonth() == 1. However, the method is very inefficient, because it checks each day of the twentieth century, while it only needs to check the first day of each month. The abovementioned code is approximately 40 times faster on my machine.

Here is an equivalent of the abovementioned code, with functional style:

long count = Stream.iterate(YearMonth.of(1901, 1), ym -> ym.plusMonths(1))
    .takeWhile(ym -> ym.isBefore(YearMonth.of(2001, 1)))
    .map(ym -> ym.atDay(1).getDayOfWeek())
    .filter(DayOfWeek.SUNDAY::equals)
    .count();

Using Todd's Java-TimeStream, with functional style:

YearMonthStream
    .from(YearMonth.of(1901, 1))
    .until(YearMonth.of(2000, 12))
    .stream()
    .map(ym -> ym.atDay(1).getDayOfWeek())
    .filter(DayOfWeek.SUNDAY::equals)
    .count();
like image 194
MC Emperor Avatar answered Oct 19 '22 00:10

MC Emperor


I see some mistakes:

public static void main(String[] args) {
    int count, sum = 0;           
    for (int i = 1901; i < 2001; i++) { // There is a mistake here, I dont know what you want to compute in this loop!
        LocalDate test = LocalDate.of(i,1,1);
        sum += test.lengthOfYear();
    }
    for (int i = 1; i < sum; i++) {
        LocalDate date1 = LocalDate.of(1901,1,1); // There is a mistake here, date1 must be outside of this loop
        date1 = date1.plusDays(i); // There is a mistake here, plusDays why?? 
    if(date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY) { // There is a mistake here, why are you cheking this: date1.getMonth() == JANUARY ?
        count++;
        }
    }
    System.out.println(count);
}

A simple solution:

public static void main(String[] args) {
    int count = 0;
    LocalDate date1 = LocalDate.of(1901, Month.JANUARY, 1);
    LocalDate endDate = LocalDate.of(2001, Month.JANUARY, 1);
    while (date1.isBefore(endDate)) {
        date1 = date1.plusMonths(1);
        if (date1.getDayOfWeek() == DayOfWeek.SUNDAY) {
            count++;
        }
    }
    System.out.println(count);
}
like image 30
David Pérez Cabrera Avatar answered Oct 18 '22 23:10

David Pérez Cabrera


Apart from the error that has already been flagged, you could reconsider your design and use the YearMonth class which seems better suited to your use case than LocalDate:

public static void main(String[] args) {
  YearMonth start = YearMonth.of(1901, 1);
  YearMonth end = YearMonth.of(2000, 12);

  int count = 0;
  for (YearMonth ym = start; !ym.isAfter(end); ym = ym.plusMonths(1)) {
    //is first day of month a sunday?
    if (ym.atDay(1).getDayOfWeek() == SUNDAY) count ++;
  }

  System.out.println(count); //171
}
like image 6
assylias Avatar answered Oct 19 '22 00:10

assylias