3 ms·
Hmm, thinking about this, you get the day of the week w plus the day number d of the last day of the month. If d=28, then return 4. Otherwise, assuming w=0 is M
by dhosek 5y ago
Hmm, thinking about this, you get the day of the week w plus the day number d of the last day of the month. If d=28, then return 4. Otherwise, assuming w=0 is Monday,¹ we'll then calculate wʹ = w - (d - 29). Rebase everything mod 7 and if dʹ ≤ 3 ≤ d, there are 5 Sundays, otherwise there are four.
⸻⸻⸻
1. If w=0 is Sunday, we'll add 6 ≡ -1 (mod 7) to w to rebase our modulo arithmetic.