Sometimes we run away with ourselves ...
You don't have to test all years, nor 400 years, not even 14 years or 7!
Considering just one year (any year you wish) is enough.
This might seem surprising, but it really isn't. As long as you don't start your year on the 1st of January, but on the 1st of March.
Suppose, the 1st of March is a Monday then:
- 31st March is a Wednesday (30 days further, so 30 mod 7 = 2; 1 + 2 = 3 => Wednesday)
- 31st of May is a Monday (91 days after 1st of March: 91 mod 7 = 0; 1 + 0 = 1 => Monday)
- 31st of July is a Saturday (152 days after 1st of March; 152 mod 7 = 5)
- 31st of August is a Tuesday (183 days after 1st of March, 183 mod 7 = 1)
- 31st of October is a Sunday (244 days after 1st of March, 244 mod 7 = 6)
- 31st of December is a Friday (305 days since 1st of March, 305 mod 7 = 4)
- 31st of January is a Monday (336 since 1st of March of the previous year, 336 mod 7 = 0)
As you can see there is never a "+3" in the above sequence, meaning that the 31st will miss "day + 3" (where day 1 is the day of 1st of March).
Because of leap years the last day of January can at the most shift one day, but as it is on the "+0" spot, it can never make it to the "+3" spot, hence "day +3" is always missing for the 31st.
QED
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.