Calculating Easter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gustavo G. Rondina

    #1

    Calculating Easter

    It is possible to caclulate every year's easter using simple
    mathematical operations. Here is a code that does the trick:



    I found the math scheme in an american scientific issue, march
    2001. The article about the calendars is very interesting.

    Have fun...
    --
    Gustavo G. Rondina

  • E. Robert Tisdale

    #2
    Re: Calculating Easter

    Gustavo G. Rondina wrote:
    [color=blue]
    > It is possible to caclulate every year's easter using simple
    > mathematical operations. Here is a code that does the trick:
    >
    > http://www.brlivre.org/c/easter.c
    >
    > I found the math scheme in an american scientific issue, march
    > 2001. The article about the calendars is very interesting.[/color]
    [color=blue]
    > cat easter.c[/color]
    // Calculating Easter

    #include <stdio.h>
    #include <stdlib.h>

    static const
    char *month_array[] = {
    "January", "February", "March",
    "April", "May", "June",
    "July", "August", "September" ,
    "October", "November", "December" };

    typedef struct Date {
    int year;
    int month;
    int day;
    } Date;

    inline static
    Date Date_create(int year, int month, int day) {
    Date d;
    d.year = year;
    d.month = month;
    d.day = day;
    return d;
    }

    inline static
    void Date_destroy(co nst Date* p) {
    }

    Date easter(int x) {

    int a = x%19;
    int b = x/100;
    int c = x%100;
    int h = (19*a + b - (b/4) - (8*b + 13)/25 + 15)%30;
    int m = (a + 11*h)/319;
    int l = (2*(b%4) + 2*(c/4) - c%4 - h + m + 32)%7;
    int n = (h - m + l + 90)/25;
    int p = (h - m + l + n + 19)%32;

    return Date_create(x, n - 1, p);
    }

    int main(int argc, char *argv[]) {
    int year; /* year to determine easter */

    if (argc == 1) {
    printf("Year: ");
    scanf("%d", &year);
    }
    else
    year = atoi(argv[1]);

    Date d = easter(year);
    printf("Easter %d is %s %d.\n",
    d.year, month_array[d.month], d.day);
    Date_destroy(&d );

    return EXIT_SUCCESS;
    }
    [color=blue]
    > gcc -Wall -std=c99 -pedantic -o easter easter.c
    > ./easter 2004[/color]
    Easter 2004 is April 11.

    Comment

    • Shug Boabie

      #3
      Re: Calculating Easter

      E. Robert Tisdale wrote:[color=blue]
      > Gustavo G. Rondina wrote:[color=green]
      > > It is possible to caclulate every year's easter using simple
      > > mathematical operations. Here is a code that does the trick:[/color][/color]
      <snip>[color=blue]
      > char *month_array[] = {
      > "January", "February", "March",
      > "April", "May", "June",
      > "July", "August", "September" ,
      > "October", "November", "December" };[/color]

      Easter can never occur before March 22 or later than April 25. So why the
      redundant code?

      Comment

      Working...