TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
easter.cpp
1/*
2 * @file
3 * @brief Determines the [Date of
4 * Easter](https://en.wikipedia.org/wiki/Date_of_Easter) after 1582
5 *
6 * @details
7 * The date of Easter is determined in each year through a calculation known as
8 * "computus." Easter is celebrated on the first Sunday after the Paschal full
9 * moon, which is the first full moon on or after 21 March. Determining this
10 * date in advance requires a correlation between the lunar months and the solar
11 * year, while also accounting for the month, date, and weekday of the Julian or
12 * Gregorian calendar. The complexity of the algorithm arises because of the
13 * desire to associate the date of Easter with the date of the Jewish feast of
14 * Passover which, Christians believe, is when Jesus was crucified.
15 *
16 *
17 * @author [AlternateWalls](https://github.com/AlternateWalls)
18 */
19
20#include <cassert>
21#include <cstdint>
22#include <iostream>
23
24/*
25 * @brief Contains information for Easter date
26 */
28 public:
29 uint64_t year;
30 uint64_t month;
31 uint64_t day;
32
33 EasterYearMonthDay(uint64_t newYear, uint64_t newMonth, uint64_t newDay) {
34 year = newYear; // Assigns year to class
35 month = newMonth;
36 day = newDay;
37 }
38};
39
40/*
41 * @brief Function that finds the month and day of Easter
42 * @params param1 An int "y" of the year you want to find Easter on after
43 * 1582
44 * @returns An instance of the easterYearMonthDay calss that contains the
45 * information (Ex. 420 - 4/20 or April 20th)
46 */
47EasterYearMonthDay findEaster(uint64_t y) {
48 if (y > 1582) {
49 uint64_t a = y % 19; // Year's location on Metonic Calendar
50 uint64_t b = y / 100; // Century index
51 uint64_t c = y % 100;
52 uint64_t d = b / 4;
53 uint64_t e = b % 4; // Takes into account leap years
54 uint64_t f = (b + 8) / 25;
55 uint64_t g = (b - f + 1) / 3;
56 uint64_t h = (19 * a + b - d - g + 15) %
57 30; // Days from Mar. 21st until the full moon
58 uint64_t i = c / 4;
59 uint64_t k = c % 4;
60 uint64_t r =
61 (32 + 2 * e + 2 * i - h - k) %
62 7; // The number of days from Paschal full moon to next Sunday
63 uint64_t m = (a + 11 * h + 22 * r) / 451;
64 uint64_t n = (h + r - 7 * m + 114) / 31; // Month of Easter
65 uint64_t p = (h + r - 7 * m + 114) % 31; // p + 1 is the day of Easter
66
67 // Assign values
69 y, n, p + 1); // Assign values to new instance of class
70
71 // Return date
72 return date;
73 } else {
74 EasterYearMonthDay date(0, 0, 0);
75
76 // Return date
77 return date;
78 }
79}
80
85static void test() {
86 // 2003 | April 20th
87 assert(findEaster(2003).month == 4); // Should return true
88 assert(findEaster(2003).day == 20); // Should return true
89
90 // 1910 | March 27th
91 assert(findEaster(1910).month == 3); // Should return true
92 assert(findEaster(1910).day == 27); // Should return true
93
94 // 1877 | April 1st
95 assert(findEaster(1877).month != 3); // Should return true
96 assert(findEaster(1877).day != 22); // Should return true
97
98 // 1400 | Invalid date
99 assert(findEaster(1400).month == 0); // Should return true
100 assert(findEaster(1400).day == 0); // Should return true
101}
102
107int main() {
108 test(); // run self-test implementations
109 return 0;
110}
void test()
for assert
Definition easter.cpp:27
uint64_t month
month Easter is on
Definition easter.cpp:30
uint64_t year
year Easter is on
Definition easter.cpp:29
uint64_t day
day Easter is on
Definition easter.cpp:31
double k(double x)
Another test function.
double g(double x)
Another test function.
double f(double x)
A function f(x) that will be used to test the method.
int main()
Main function.
int h(int key)