A formula called Zeller\'s congruence may be used to compute the day of the week
ID: 1839756 • Letter: A
Question
A formula called Zeller's congruence may be used to compute the day of the week, given the date (within a certain range): f = 1 + ([2.6m - 0.2] + k + y + [y/4) + [c/4] - 2c) modulo 7 where the brackets denote the integer part; modulo 7 means the remainder when divided by 7; and m is the month number, with January and February taken as months 11 and 12 of the preceding year (March is then month 1 and December is month 10). k is the day of the month. c is the century. y is the year in the century. f = 1 is Sunday (f = 2 is Monday, etc). For example, 23rd August 1963 is represented by m = 6, k = 23, c = 19, y = 63; 1st January 1800 is represented by m = 11. k = 1. c = 17, y = 99. Write a function day of wee k that takes the date in the form of a vector d = [dd mm yyyy] (e.g., [9 3 2001 ] for 9 March 2001) and returns the day of the week (in words) on which it falls. Test your program on some known dates such as today, your birthday, or 7 December 1941 (Pearl Harbor). The formula will not work if you go too far back. Shakespeare and Cervantes both died on 23 April 1616-Shakespeare, on a Tuesday; Cervantes, on a Saturday! This is because England had not yet adopted the Gregorian calendar and was consequently ten days behind the rest of the world. The formula will also not work if you go too far forward.Explanation / Answer
function f = dayofweek(d)
if d(2)<=2
d(3)=d(3)-1;
d(2)=d(2)+10;
else
d(2)=d(2)-2;
end
m=d(2);
k=d(1);
c=fix(d(3)./100);
y=rem(d(3),100);
f1= 1+(fix(2.6*m-0.2)+k+y+fix(y/4)+fix(c/4)-(2*c));
f1=rem(f1,7);
week={'sunday' 'monday' 'tuesday' 'wednesday' 'thursday' 'friday' 'saturday'};
f=week(f1);
>> dayofweek([14 4 2016])
ans =
'thursday'
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.