Project Euler Homework
Functions

019.cpp File Reference

How many Sundays fell on the first of the month during the twentieth century? More...

#include <iostream>

Go to the source code of this file.

Functions

unsigned short weekday (int DD, int MM, int YYYY)
 Zeller algorithm.
int main ()

Detailed Description

How many Sundays fell on the first of the month during the twentieth century?

You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

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

Definition in file 019.cpp.


Function Documentation

unsigned short weekday ( int  DD,
int  MM,
int  YYYY 
)

Zeller algorithm.

Use the Zeller algorithm to find the week-day of a particular day:

\[ W = \left [\frac{CC}{4} \right ] -2 CC+YY+\left [\frac{YY}{4} \right ]+\left [\frac{26(MM+1)}{10} \right ]+DD-1 (\textrm{mod }7) \]

  • [x] means to find the greatest integer smaller than x.
  • x (mod 7) means to find the positive integral remainder when x is divided by 7.
  • DD = day of month, CCYY = year
  • MM is value representing month (Mar=3,Apr=4,...,Dec=12; Jan and Feb are counted as month 13 & 14 of the previous year.)
  • W=0 means Sunday, 1 means Monday, etc.
  • This formula is valid except before 15/10/1802, which W should be deducted by 10.

e.g. on the day 13 Jun 2009,

\[ W = \left [\frac{20}{4} \right ] -2\times 20+9+\left [\frac{9}{4} \right ]+\left [\frac{26(6+1)}{10} \right ]+13-1 (\textrm{mod }7)=6 \]

So that day was Saturday.

In programming, the algorithm is slightly modified because the different modulo algorithm:

\[ W = \left [\frac{CC}{4} \right ] +5 CC+YY+\left [\frac{YY}{4} \right ]+\left [\frac{26(MM+1)}{10} \right ]+DD-1 (\textrm{mod }7) \]

If modified year is used instead, the algorithm can be made simpler:

\[ W = YYYY+\left [\frac{YYYY}{4} \right ]+6 \left [\frac{YYYY}{100} \right ]+\left [\frac{YYYY}{400} \right ]+\left [\frac{26(MM+1)}{10} \right ]+DD-1 (\textrm{mod }7) \]

Source: Wikipedia

Definition at line 46 of file 019.cpp.

 All Classes Files Functions Variables