Krok 6: algorytm Meeusa i porównanie na 200 latach

Kalendarz ze świętami, notatkami i językami - Kalendarz w JavaScript krok po kroku (JavaScript)

Wersja Gaussa ze stałymi 24 i 5 działa tylko w latach 1900-2099. Algorytm Meeusa (Jonesa-Butchera) sam uwzględnia stulecia (zmienne `b`, `f`, `g` to poprawki kalendarza gregoriańskiego i Księżyca) i nie ma wyjątków. Najlepszy test: policzyć Wielkanoc oboma sposobami dla każdego roku 1900-2099 i policzyć różnice.

const WEEKDAYS = ['niedziela', 'poniedziałek', 'wtorek', 'środa', 'czwartek', 'piątek', 'sobota'];
const pad = n => String(n).padStart(2, '0');
const fmt = date => `${pad(date.getDate())}.${pad(date.getMonth() + 1)}`;
// algorytm Meeusa (Jonesa-Butchera): Wielkanoc dla dowolnego roku kalendarza gregoriańskiego
function easter(year) {
  const a = year % 19;
  const b = Math.floor(year / 100);
  const c = year % 100;
  const d = Math.floor(b / 4);
  const e = b % 4;
  const f = Math.floor((b + 8) / 25);
  const g = Math.floor((b - f + 1) / 3);
  const h = (19 * a + b - d - g + 15) % 30;
  const i = Math.floor(c / 4);
  const k = c % 4;
  const l = (32 + 2 * e + 2 * i - h - k) % 7;
  const m = Math.floor((a + 11 * h + 22 * l) / 451);
  const month = Math.floor((h + l - 7 * m + 114) / 31);
  const day = ((h + l - 7 * m + 114) % 31) + 1;
  return new Date(year, month - 1, day);
}
function easterGaussFixed(year) {
  const a = year % 19;
  const b = year % 4;
  const c = year % 7;
  const d = (19 * a + 24) % 30;
  const e = (2 * b + 4 * c + 6 * d + 5) % 7;
  if (d === 29 && e === 6) return new Date(year, 3, 19);
  if (d === 28 && e === 6 && a > 10) return new Date(year, 3, 18);
  return new Date(year, 2, 22 + d + e);
}

let differences = 0;
for (let year = 1900; year <= 2099; year++) {
  if (easter(year).getTime() !== easterGaussFixed(year).getTime()) differences++;
}
console.log(`Lata 1900-2099, różnic: ${differences}`);
for (const year of [1700, 1954, 2026, 2285]) {
  console.log(`${year}: ${fmt(easter(year))} (${WEEKDAYS[easter(year).getDay()]})`);
}

Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.

Wszystkie lekcje kursu Kalendarz w JavaScript krok po kroku·Zaloguj się, aby zapisać postęp na koncie.