Talk:Day of the week: Difference between revisions

no edit summary
No edit summary
 
Line 204:
::For a larger task such as [[Range expansion]] you might prefer to work on the one example and edit it to come to a consensus on what looping structure to use. But an example picking and testing characters from a string might go ''alongside'' another example that parses using a regexp as the two methods are sufficiently different to be noteworthy.
::I don't know how to write the rules of noteworthiness other than by reading a lot of RC edits and looking at what happens. This will tend to perpetuate the status quo, however discussions like this pop up to help :-) (P.S. Edited simultaneousely with Michael's edit above. Written to follow the comment of Underscore)<br>--[[User:Paddy3118|Paddy3118]] 19:47, 1 August 2010 (UTC)
 
== Month contribution ==
Although date calculations don't usually need much optimization, it may be interesting to note that the division by 10 in Zeller's congruence (useful at Zeller time for hand calculations), can be replaced by a shift. Keeping only the interesting part, all it does is this :
 
Count days for 1st of each month, from 1st march, modulo 7 (in the following list, the third column is the number of days of the month, and the fourth is the cumulated number of days, mod 7, with a 1 month lag since march starts at day 0)
<lang>3 march 31 0
4 april 30 3 = 31 mod 7
5 may 31 5 = (31 + 30) mod 7
...
12 december 31 2 = (31 + 30 + ... + 30) mod 7 // the last in the sum is november, remember the lag
13 january 31 5 = ...
14 february -- 1 = ...</lang>
 
For february, one does not need to know the number of days, and it's precisely because it can change that we use a trick (year-1, month+12) to push it at the end of the list.
 
So, all one really needs is a mapping from (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) to (0, 3, 5, 1, 3, 6, 2, 4, 0, 2, 5, 1), possibly up to a constant, mod 7.
As we know from Zeller's congruence, '''floor((26*(m+1))/10)''' works perfectly well.
But so does '''floor((83*m+24)/32)''', and division by 32 is simply a shift.
Of course, there are many other values of A, B, C such that floor((A*m+B)/C) does the job, but 32 is the least power of 2 that works.
 
[[User:Dejvicka|Dejvicka]] 12:54, 22 September 2012 (UTC)
Anonymous user