Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Good Friday & Easter Sunday

Title: Good Friday & Easter Sunday. Question: How can I find the date of a the Good Friday & Easter Sunday Answer: This is my implementation of an algorithm, which calculates the date of Good Friday & Easter Sunday for a given year. The algorithm also returns other information: the Epact, the dominical letter and the Golden number. All these were used to help calculate the date. The procedure CalculateEaster does all the calculation and return the date of Easter Sunday and the epact, the dominical letter and the golden number. I use the procedure Output to encode the information into strings, the important point here is calculating the date of Good Friday when Easter Sunday is the first or the second of April(e.g. 6 AD). The source and a demo application is attached to the article Source: unit unit1; interface uses Classes; procedure CalculateEaster(year : Integer; Var day, month, GoldenNum, Epact, Dominical : Integer); procedure Output(OutputString : TStrings; year, day, month, GoldenNum, Epact, Dominical : Integer); implementation uses SysUtils; const YearsString = 'In the year %d AD:'; FridayString = ' Good Friday was on the %d of %s'; DateString = ' Easter Sunday was on the %d of %s'; GoldenNumString = ' The Golden number was %d'; EpactString = ' The Epact was %d'; DominicalString = ' The Dominical letter was %s'; CutString = '*-----------------------------------------------------*'; April = 'April'; March = 'March'; /////////////////////////////////////////////////////// { CalculateEaster : calculates the date of Easter Sunday, also the year's Golden Number, Epact and Dominical letter. } procedure CalculateEaster(year : Integer; Var day, month, GoldenNum, Epact, Dominical : Integer); var A, B, C, D, E, G, H, M, J, K, L, N, P : Integer; begin A := year Mod 19; B := year Div 100; C := year Mod 100; D := B Div 4; E := B Mod 4; G := (8*B + 13) Div 25; H := (19*A + B - D - G + 15) Mod 30; M := (A + 11*H) Div 319; J := C Div 4; K := C Mod 4; L := (2*E + 2*J - K - H + M + 32) Mod 7; N := (H - M + L + 90) Div 25; P := (H - M + L + N + 19) Mod 32; day := P; month := N; GoldenNum := A + 1; if H Epact := 23 - H Else Epact := 53 - H; Dominical := (2*E + 2*J - K) Mod 7; if Dominical Inc(Dominical,7); end;{ CalculateEaster } /////////////////////////////////////////////////////// { Output : writes the output of CalculateEaster to a string list. } procedure Output(OutputString : TStrings; year, day, month, GoldenNum, Epact, Dominical : Integer); var S, S2 : String; Fday : Integer; begin OutputString.Add(Format(YearsString,[year])); // convert month if month = 3 then S := March Else S := April; //finde when was the good friday Fday := day - 2; S2 := S; if Fday = 0 then begin Fday := 31; S2 := March; end; if Fday = -1 then begin Fday := 30; S2 := March; end; OutputString.Add(Format(FridayString,[Fday,S2])); OutputString.Add(Format(DateString,[day,S])); OutputString.Add(Format(GoldenNumString,[GoldenNum])); OutputString.Add(Format(EpactString,[Epact])); OutputString.Add(Format(DominicalString,[chr(Dominical + 65)])); OutputString.Add(CutString); end;{ Output } end.