How to resolve the algorithm Holidays related to Easter step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Holidays related to Easter step by step in the Kotlin programming language

Table of Contents

Problem Statement

Calculate the dates of:

As an example, calculate for the first year of each century from; From the year 325 CE on,   Easter Sunday   has been defined as the first Sunday after the first full moon on or after the day of the March equinox. However, the actual astronomical values for the moments of the full moon and equinox are not used. Instead, approximations are used, the first one being that the equinox is assumed to fall on March 21st every year. The tracking of the moon phases is similarly done with relatively straightforward arithmetic (compared to the sort required for astronomical accuracy) which amounts to maintaining a lunisolar calendar in parallel to our standard purely-solar one. When Pope Gregory reformed the Catholic calendar in 1582 CE, the drifting of Easter with respect to the seasons was the driving motivation, and the rules for determining it (called the computus) were altered to correct that drift. Catholic nations adopted both the new calendar and the new computus right away, while Western Protestant nations adopted them more gradually over the next 350 years or so. Eventually, even nations dominated by the Eastern Orthodox church adopted a similar calendar reform (the Revised Julian calendar), so pretty much the whole world agrees on what day it is for civil purposes. But the Eastern churches never adopted the corresponding Easter rule changes; they still use the original Julian calendar and computus to determine the date of what is known in the West as "Orthodox Easter". Therefore, your output should indicate which computus was used to calculate the dates and, at least for historical dates where the calendar can't be assumed or is location-dependent, which calendar those dates are given in. You may find algorithms on the Computus Wikipedia page. Some of the results: In the year 400 CE, Easter Sunday was April 1st (in the contemporary Julian calendar), making Ascension Thursday May 10th and Pentecost May 20th. It is ahistorical to give a date so far back for either Trinity Sunday or Corpus Christi, neither of which were observed until centuries later, but they would have been May 27th and 31st. If you extend the modern civil calendar back that far, those days are instead assigned the subsequent dates: Easter on April 2nd, Ascension on May 11th, Pentecost on May 21st. Skipping forward to the year 2100 CE, assuming the rules don't change between now and then, the Western churches will observe Easter on March 28, Ascension Thursday May 6th, Pentecost May 16th, Trinity Sunday May 23rd and Corpus Christi May 27th. Heading East, the Orthodox rules place Easter on April 18 in the original Julian calendar; the corresponding civil date is May 2nd. That puts the Ascension on June 10th and Pentecost June 20th. Orthodox Trinity Sunday is the same day as Pentecost, but they observe All Saints' Sunday the following week, June 27th. Corpus Christi is a purely Catholic date that has no Orthodox version.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Holidays related to Easter step by step in the Kotlin programming language

Kotlin program to calculate and print Christian holidays for a given year range.

  1. The program imports necessary packages and defines a constant list of holiday offsets. The offsets are the number of days from Easter to the other holidays.

  2. The program defines a function String.padCenter(n) that pads a string with spaces to center it within a field of width n.

  3. The program defines a function calculateEaster(year) that calculates the date of Easter for the given year using the algorithm described by the Gregorian calendar.

  4. The program defines a function outputHolidays(year) that prints the dates of Easter and other Christian holidays for the given year.

  5. The main function loops through a range of years and calls outputHolidays(year) for each year.

Sample output:

Year  Easter  Ascension  Pentecost  Trinity  C/Christi
CE   Sunday  Thursday    Sunday    Sunday   Thursday 
----  ------  ---------  ---------  -------  ---------
400  Mar 27  May 5       May 15     May 22     May 26
500  Apr 16  May 4       May 14     May 21     May 25
600  Apr 23  May 11      May 21     May 28     Jun 1
700  Apr 9   Apr 27      May 7      May 14     May 18
800  Mar 31  May 9       May 19     May 26     May 30
900  Apr 14  May 2       May 12     May 19     May 23
1000  Apr 6   Apr 24      May 4      May 11     May 15
1100  Mar 29  May 7       May 17     May 24     May 28
1200  Apr 19  May 9       May 19     May 26     May 30
1300  Apr 7   Apr 25      May 5      May 12     May 16
1400  Mar 30  May 8       May 18     May 25     May 29
1500  Apr 13  May 1       May 11     May 18     May 22
1600  Apr 4   Apr 22      May 2      May 9      May 13
1700  Mar 27  May 5       May 15     May 22     May 26
1800  Apr 16  May 4       May 14     May 21     May 25
1900  Mar 31  May 9       May 19     May 26     May 30
2000  Apr 14  May 2       May 12     May 19     May 23
2010  Apr 4   Apr 22      May 2      May 9      May 13
2011  Apr 17  May 5       May 15     May 22     May 26
2012  Apr 8   Apr 26      May 6      May 13     May 17
2013  Mar 31  May 9       May 19     May 26     May 30
2014  Apr 13  May 1       May 11     May 18     May 22
2015  Apr 5   Apr 23      May 3      May 10     May 14
2016  Mar 27  May 5       May 15     May 22     May 26
2017  Apr 16  May 4       May 14     May 21     May 25
2018  Mar 31  May 9       May 19     May 26     May 30
2019  Apr 14  May 2       May 12     May 19     May 23
2020  Apr 5   Apr 23      May 3      May 10     May 14

Source code in the kotlin programming language

// version 1.1.2

import java.util.Calendar
import java.util.GregorianCalendar

val holidayOffsets = listOf(
    "Easter" to 0,
    "Ascension" to 39,
    "Pentecost" to 49,
    "Trinity" to 56,   
    "C/Christi" to 60
)

fun String.padCenter(n: Int): String {
    val len = this.length
    if (n <= len) return this
    return this.padStart((n + len) / 2).padEnd(n)
}

fun calculateEaster(year: Int): GregorianCalendar {
    val a = year % 19
    val b = year / 100
    val c = year % 100
    val d = b / 4
    val e = b % 4
    val f = (b + 8) / 25
    val g = (b - f + 1) / 3
    val h = (19 * a + b - d - g + 15) % 30
    val i = c / 4
    val k = c % 4
    val l = (32 + 2 * e + 2 * i - h - k) % 7
    val m = (a + 11 * h + 22 * l) / 451
    val n = h + l - 7 * m + 114
    val month = n / 31 - 1  // months indexed from 0 
    val day = (n % 31) + 1
    return GregorianCalendar(year, month, day)
}

fun outputHolidays(year: Int) {
    val date = calculateEaster(year)
    print("%4d  ".format(year))
    var po = 0
    for ((h, o) in holidayOffsets) {
        date.add(Calendar.DATE, o - po)
        po = o
        print("${"%1\$td %1\$tb".format(date).padCenter(h.length)}  ")
    }
    println()
}

fun main(args: Array<String>) {
    println("Year  Easter  Ascension  Pentecost  Trinity  C/Christi")
    println(" CE   Sunday  Thursday    Sunday    Sunday   Thursday ") 
    println("----  ------  ---------  ---------  -------  ---------")  
    for (year in 400..2100 step 100) outputHolidays(year)
    println()
    for (year in 2010..2020) outputHolidays(year)
}


  

You may also check:How to resolve the algorithm Animate a pendulum step by step in the Scilab programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Microsoft Small Basic programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the PL/0 programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Lua programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the F# programming language