How to calculate the Easter date for a given year using Gauss' Algorithm - GeeksforGeeks

08 Mar, 2022
Given an integer Y, denoting a year, as input, the task is to find the date of Easter for that year.
Examples: 
Input: Y = 2020 Output: 2020-04-12 Explanation: In 2020, Easter was on 12 April Hence, Easter date will be 2020-04-12
Input: Y = 1991 Output: 1991-03-30 Explanation: In 1991, Easter was on 30 March Hence, Easter date will be 1991-03-30 
Approach: Gauss Easter Algorithm is used to easily calculate the date of Easter for a year. This algorithm was first thought of by Carl Friedrich Gauss. There is no proper explanation on how Gauss landed up with this algorithm, but the implementation of this algorithm has proved to be very accurate. A detailed explanation of the Gauss Easter Algorithm is as follows: 
First, calculate the location of the year Y in the Metonic cycle.
A = Y mod 19
Now, find the number of leap days according to Julian’s calendar.
B = Y mod 4
Then, let’s take into account that the non-leap year is one day longer than 52 weeks.
C = Y mod 7
M depends on the century of year Y. For 19th century, M = 23. For the 21st century, M = 24 and so on. It is calculated using the following relations:
P = floor (Y / 100) Q = ((13 + 8 * P) / 25) M = (15 – Q + P – (P / 4)) mod 30 
The difference between the number of leap days between the Julian and the Gregorian calendar is given by:
N = (4 + P – (P / 4)) mod 7
The number of days to be added to March 21 to find the date of the Paschal Full Moon is given by:
D = (19*A + M) mod 30
And, the number of days from the Paschal full moon to the next Sunday is given by:
E = (N + 2*B + 4*C + 6*D) mod 7 
Therefore, using D and E, the date of Easter Sunday is going to be March (22 + D + E). If this number comes out to be greater than 31, then we move to April.
Now the lunar month is not exactly 30 days but a little less than 30 days. So to nullify this inconsistency, the following cases are followed:
if (D == 29) and (E == 6) return “April 19” else if (D == 28) and (E == 6) return “April 18” 
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the// above approach #include <iostream>#include <math.h> using namespace std; // Function calculates and prints// easter date for given year Yvoid gaussEaster(int Y){    float A, B, C, P, Q,        M, N, D, E;     // All calculations done    // on the basis of    // Gauss Easter Algorithm    A = Y % 19;    B = Y % 4;    C = Y % 7;    P = (float)floor(Y / 100);    Q = (float)floor((13 + 8 * P) / 25);    M = (int)(15 - Q + P - P / 4) % 30;    N = (int)(4 + P - P / 4) % 7;    D = (int)(19 * A + M) % 30;    E = (int)(2 * B + 4 * C + 6 * D + N) % 7;    int days = (int)(22 + D + E);     // A corner case,    // when D is 29    if ((D == 29) && (E == 6)) {        cout << Y << "-04-19";        return;    }    // Another corner case,    // when D is 28    else if ((D == 28) && (E == 6)) {        cout << Y << "-04-18";        return;    }    else {        // If days > 31, move to April        // April = 4th Month        if (days > 31) {            cout << Y << "-04-"                 << (days - 31);            return;        }        else {            // Otherwise, stay on March            // March = 3rd Month            cout << Y << "-03-"                 << days;            return;        }    }} // Driver Codeint main(){    int Y = 2020;    gaussEaster(Y);     return 0;}
// Java program for the// above approach import java.util.Date;import java.util.Scanner; // Function calculates and prints// easter date for given year Ypublic class GaussEaster {     static void gaussEaster(int Y)    {        float A, B, C, P, Q,            M, N, D, E;         // All calculations done        // on the basis of        // Gauss Easter Algorithm        A = Y % 19;        B = Y % 4;        C = Y % 7;        P = (float)Math.floor(Y / 100);        Q = (float)Math.floor(            (13 + 8 * P) / 25);        M = (15 - Q + P - P / 4) % 30;        N = (4 + P - P / 4) % 7;        D = (19 * A + M) % 30;        E = (2 * B + 4 * C + 6 * D + N) % 7;        int days = (int)(22 + D + E);         // A corner case,        // when D is 29        if ((D == 29) && (E == 6)) {            System.out.println(Y + "-04"                               + "-19");            return;        }        // Another corner case,        // when D is 28        else if ((D == 28) && (E == 6)) {            System.out.println(Y + "-04"                               + "-18");            return;        }        else {             // If days > 31, move to April            // April = 4th Month            if (days > 31) {                System.out.println(Y + "-04-"                                   + (days - 31));                return;            }            // Otherwise, stay on March            // March = 3rd Month            else {                System.out.println(Y + "-03-"                                   + days);                return;            }        }    }     // Driver code    public static void main(String[] args)    {        int Y = 2020;        gaussEaster(Y);    }}
# Python3 program for the# above approachimport math # Function calculates and prints# easter date for given year Ydef gaussEaster(Y):       # All calculations done    # on the basis of    # Gauss Easter Algorithm    A = Y % 19    B = Y % 4    C = Y % 7         P = math.floor(Y / 100)    Q = math.floor((13 + 8 * P) / 25)    M = (15 - Q + P - P // 4) % 30    N = (4 + P - P // 4) % 7    D = (19 * A + M) % 30    E = (2 * B + 4 * C + 6 * D + N) % 7    days = (22 + D + E)      # A corner case,    # when D is 29    if ((D == 29) and (E == 6)):        print(Y, "-04-19")        return         # Another corner case,    # when D is 28    elif ((D == 28) and (E == 6)):        print(Y, "-04-18")        return         else:                 # If days > 31, move to April        # April = 4th Month        if (days > 31):            print(Y, "-04-", (days - 31))            return                 else:                         # Otherwise, stay on March            # March = 3rd Month            print(Y, "-03-", days)            return         # Driver CodeY = 2020 gaussEaster(Y) # This code is contributed by code_hunt
// C# program for the// above approachusing System;class GFG{ // Function calculates and prints// easter date for given year Ystatic void gaussEaster(int Y){    float A, B, C, P, Q,            M, N, D, E;     // All calculations done    // on the basis of    // Gauss Easter Algorithm    A = Y % 19;    B = Y % 4;    C = Y % 7;    P = (float)(Y / 100);    Q = (float)(        (13 + 8 * P) / 25);    M = (15 - Q + P - P / 4) % 30;    N = (4 + P - P / 4) % 7;    D = (19 * A + M) % 30;    E = (2 * B + 4 * C + 6 * D + N) % 7;    int days = (int)(22 + D + E);     // A corner case,    // when D is 29    if ((D == 29) && (E == 6))    {        Console.Write(Y + "-04" + "-19");        return;    }       // Another corner case,    // when D is 28    else if ((D == 28) && (E == 6))    {          Console.Write(Y + "-04" + "-18");          return;    }    else    {         // If days > 31, move to April        // April = 4th Month        if (days > 31)        {            Console.Write(Y + "-04-" +                         (days - 31));            return;        }        // Otherwise, stay on March        // March = 3rd Month        else        {            Console.Write(Y + "-03-" + days);            return;        }    }} // Driver codepublic static void Main(string[] args){    int Y = 2020;    gaussEaster(Y);}} // This code is contributed by Ritik Bansal
<script>// Javascript implementation for the above approach // Function calculates and prints// easter date for given year Y     function gaussEaster(Y)    {        let A, B, C, P, Q,            M, N, D, E;         // All calculations done        // on the basis of        // Gauss Easter Algorithm        A = Y % 19;        B = Y % 4;        C = Y % 7;        P = Math.floor(Y / 100);        Q = Math.floor(            (13 + 8 * P) / 25);        M = (15 - Q + P - P / 4) % 30;        N = (4 + P - P / 4) % 7;        D = (19 * A + M) % 30;        E = (2 * B + 4 * C + 6 * D + N) % 7;        let days = (22 + D + E);         // A corner case,        // when D is 29        if ((D == 29) && (E == 6)) {            document.write(Y + "-04"                               + "-19");            return;        }        // Another corner case,        // when D is 28        else if ((D == 28) && (E == 6)) {            document.write(Y + "-04"                               + "-18");            return;        }        else {             // If days > 31, move to April            // April = 4th Month            if (days > 31) {                document.write(Y + "-04-"                                   + (days - 31));                return;            }            // Otherwise, stay on March            // March = 3rd Month            else {                document.write(Y + "-03-"                                   + days);                return;            }        }    }     // Driver Code         let Y = 2020;    gaussEaster(Y);     </script>
2020-04-12
 
Time complexity: O(1) Auxiliary Space: O(1) 
bansal_rtk_
code_hunt
sanjoy_62
simranarora5sos
Aptitude-Calendars
Algorithms
Mathematical
School Programming
Mathematical
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
DSA Sheet by Love Babbar
Difference between Informed and Uninformed Search in AI
SCAN (Elevator) Disk Scheduling Algorithms
Quadratic Probing in Hashing
K means Clustering - Introduction
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7