math - Quickest Algorithm For This Interesting Occurence -
math - Quickest Algorithm For This Interesting Occurence -
when using lunar calendar year consist of 52 weeks in us, according seven day week entry on wikipeda.
calculation assumptions 12 months x 4 weeks = 48 weeks 48 weeks x 7 days = 336 days we create 4 weeks , 1 day match lunar calender our year(365.25 - 336 = 29) 7 months have 3 days (21) 4 months have 2 extra(8) we add together 1 day feb every 4 years business relationship quarter of day. commonly referred jump yearusing these calculation assumptions, want general algorithm, regardless of language tells me months have repeating days of 5. see this question.(this gives me fridays, , don't know if reply optimized)
i want add together following: why 5 fridays, 5 saturdays , 5 sundays don’t occur every 823 years,
so given reasonable year only, algorithm returns every combination of months within year days repeat 5 times, don't have go link above figure out are.
language dependent functions , methods:
in spirit of great reply below, thought i'd add together of built-in functions jim mischel referred to:
vb.net: look first day , lastly day. c#: first day , lastly day php: love approach, relative date formats sql server: useful date functions, combined find weekdays both work in 2008-r2+ mysql: mysql obtain first day of current week, month, quarter / lastly daythose major ones. if has more, please allow me know.
note programming language these days comes library functions you. or there such library available 3rd party. if want calculate . . .
as link explains, month 31 days have 5 occurrences of 3 days. if month starts on sunday, there 5 sundays, 5 mondays, , 5 tuesdays.
if know day first day of year falls on, it's easy compute first day of month. if number days 0 sunday, 1 monday, 2 tuesday, etc., can find first day of month doing little modulo arithmetic.
the first day of feb equal first day of jan plus 31, mod 7. is:
feb1 = (jan1 + 31) % 7
so 2014 started on wednesday. day number 3.
(3 + 31) % 7 = 34 % 7 = 6 (saturday)
which can verify looking @ calendar year.
calculating other months using technique require build table contains cumulative number of days @ end of each month. that's not necessary. need table of 12 entries tell offsets each month, , little logic add together 1 months later feb in jump years.
take 2014, example. table below shows day each month starts on.
january quarta-feira (3) feb saturday (6) march saturday (6) apr tuesday (2) may th (4) june sunday (0) july tuesday (2) august fri (5) september mon (1) oct quarta-feira (3) nov saturday (6) dec monday(1)
what need offsets each month. see difference between first day of feb , first day of jan 3. same march. april, difference -1. work in modulo arithmetic, you'll want create 6. anyway, can build table tells offset need add together each month:
january 0 feb 3 march 3 apr 6 may 1 june 4 july 6 august 2 september 5 oct 0 nov 3 dec 5
given that, if know first day of year, can compute first day of month. consulting calendar, see first day of september 2014 monday. let's see if formula works. first day of jan wednesday, , offset table says need add together offset of 5 september:
(3 + 5) % 7 = 1 (monday)
for jump years, need add together 1 offset if you're looking month after february. take 2012. first day of jan sunday. using same formula above, , adding 1 jump year, september 1, 2012 was:
(0 + 5 + 1) % 7 = 6 (saturday)
the trick finding day of week jan 1, , given link in comment. after it's simple table lookup, addition, , modulo arithmetic.
by way, speed algorithm slightly creating sec offset table used jump years. table be:
january 0 feb 3 march 4 apr 0 may 2 june 5 july 0 august 3 september 6 oct 1 nov 4 dec 6
algorithm math
Comments
Post a Comment