java - Is there formula to calculate what day is for anyday? I got stuck by Project Euler # 19 -


i doing project euler #19. here states:

you given following information, may prefer research yourself.

1 jan 1900 monday. thirty days has september, april, june , november. rest have thirty-one, saving february alone, has twenty-eight, rain or shine. , on leap years, twenty-nine. leap year occurs on year evenly divisible 4, not on century unless divisible 400. how many sundays fell on first of month during twentieth century (1 jan 1901 31 dec 2000)?

i have searched in internet formula calculate day of week. found zeller formula outstanding. w = [c/4] - 2c + y + [y/4] + [13 * (m+1) / 5] + d - 1.(c century + 1, , y last 2 number of year) however, turns wrong when check 1900.01.01, it's should monday, according formula, 6(that saturday)

i have checked lot of dates, right formula. there still fewer days doesn't match. java code below case:

package number;  public class countingsundays {     public static int calculateweek(int year, int month, int date){         int c = year/100;         int y = year%100;         int w = c/4-2*c+y+y/4+13*(month+1)/5+date-1;         w = (w%7 + 7)%7;         return w;     }     public static void main(string[] args) { //      int w = calculateweek(1900, 01, 01); //      system.out.println(w);         int count = 0;         for(int = 1901; <= 2000; i++)             for(int j = 1; j <= 12; j++)                 if(calculateweek(i, j, 01) == 0)                     count++;         system.out.println(count);     } } 

for mismatch, output 173, not required result 171. can give me tips? or there wrong code?

the wikipedia article cited says

m month (3 = march, 4 = april, 5 = may, ..., 14 = february) 

and

for january 1, 2000, date treated 13th month of 1999, values be:     q = 1     m = 13     k = 99     j = 19 

so should adapt input values accordingly, if want use formula, i.e. add this

if (month <= 2) {     month += 12;     year--; } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -