Calculating Easter

G

Gustavo G. Rondina

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.

Have fun...
 
E

E. Robert Tisdale

Gustavo said:
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.
> cat easter.c
// 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(const 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;
}
> gcc -Wall -std=c99 -pedantic -o easter easter.c
> ./easter 2004
Easter 2004 is April 11.
 
S

Shug Boabie

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top