'Calculate the date of Easter Sunday
Write a program to compute the date of Easter Sunday. Easter Sunday is the first Sunday after the first full moon of spring. Use the algorithm invented by the mathematician Carl Friedrich Gauss in 1800:
- Let
ybe the year (such as 1800 or 2001) - Divide
yby19and call the remaindera. Ignore the quotient. - Divide
yby100to get a quotientband a remainderc. - Divide
bby4to get a quotientdand a remaindere. - Divide
8 * b + 13by25to get a quotientg. Ignore the remainder. - Divide
19 * a + b - d - g + 15by30to get a remainderh. Ignore the quotient. - Divide
cby4to get a quotientjand a remainderk. - Divide
a + 11 * hby319to get a quotientm. Ignore the remainder. - Divide
2 * e + 2 * j - k - h + m + 32by7to get a remainderr. Ignore the quotient. - Divide
h - m + r + 90by25to get a quotientn. Ignore the remainder. - Divide
h - m + r + n + 19by32to get a remainder ofp. Ignore the quotient.
Then Easter falls on a day p of month n.
For example, if y is 2001:
a = 6
b = 20
c = 1
d = 5
e = 0
g = 6
h = 18
j = 0
k = 1
m = 0
r = 6
n = 4
p = 15
Therefore, in 2001, Easter Sunday fell on April 15.
Make sure you prompt the user for a year and have the user input the year. Also, make sure you output the values of p and n with the appropriate messages describing the values output.
I'm having a little trouble putting this into Java code. Here's what I've tried:
import java.util.Scanner;
public class Easter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int y = 2014;
int a = y % 19;
int b = y / 100;
int c = y % 100;
int d = b / 4;
int e = b % 4;
int g = (8 * b + 13) / 25;
int h = (19 * a + b - d - g + 15) % 30;
int j = c / 4;
int k = c % 4;
int m = (a + 11 * h) / 319;
int r = (2 * e + 2 * j - k - h + m + 32) % 7;
int n = (h - m + r + 90) / 25;
int p = (h - m + r + n + 19) % 32;
getEasterSundayMonth = n;
System.out.println("Month: " + Easter.getEasterSundayMonth());
}
}
This is what I have. I don't know how to assign stuff, like I tried to get getEasterSundayMonth to equal the value of n, pretty sure its not right. Where do I go from here?
Solution 1:[1]
You aren't far from getting your program working. You really have two things left you need to do.
- Prompt the user for a year
- Output the date found
The trick to using a Scanner to prompt the user for input is to create a while-loop which tests each line the user enters, and keeps repeating until it sees a legal value.
Instead of hard-coding y = 2014; (or whatever), you want to do something like this:
Scanner input = new Scanner(System.in);
int y = -1; // No easter back in B.C.
while (y < 0) {
System.out.println("Please enter a year (integer greater than zero)");
if (input.hasNextInt()) { // check to see if the user entered a number
y = input.nextInt(); // if so, read it
}
input.nextLine(); // advance the scanner to the next line of input
}
in this case, each time the user doesn't enter a number, y remains -1 and the loop continues.
You are already doing all the calculations correctly, so to end your program, you just need to output the month/day.
I wouldn't bother trying to extract the calculation into a helper method. Just use the calculated values directly in main():
int a = y % 19;
int b = y / 100;
...
int n = (h - m + r + 90) / 25;
int p = (h - m + r + n + 19) % 32;
System.out.println("In the year " + y + " Easter with fall on day " + p + " of month " + n);
Solution 2:[2]
In case anyone is looking for the updated version (NY Anonymous Gregorian) of the algorithm in Typescript...
easterDate() {
var currentYear = new Date().getFullYear();
var a = Math.floor(currentYear % 19);
var b = Math.floor(currentYear / 100);
var c = Math.floor(currentYear % 100);
var d = Math.floor(b / 4);
var e = Math.floor(b % 4);
var f = Math.floor((b + 8) / 25);
var g = Math.floor((b - f + 1) / 3);
var h = Math.floor((19 * a + b - d - g + 15) % 30);
var i = Math.floor(c / 4);
var k = Math.floor(c % 4);
var l = Math.floor((32 + 2 * e + 2 * i - h - k) % 7);
var m = Math.floor((a + 11 * h + 22 * l) / 451);
var n = Math.floor((h + l - 7 * m + 114) / 31);
var p = Math.floor(((h + l - 7 * m + 114) % 31) + 1);
// console.log('a: ' + a + ' b: ' + b + ' c: ' + c + ' d: ' + d + ' e: ' + e);
// console.log('f: ' + f + ' g: ' + g + ' h: ' + h + ' i: ' + i + ' k: ' + k);
// console.log('l: ' + l + ' m: ' + m + ' n: ' + n + ' p: ' + p);
// console.log("In the year " + currentYear + " Easter with fall on day " + p + " of month " + n);
var month = n.toString();
while (month.length < 2) month = "0" + month;
var day = p.toString();
while (day.length < 2) day = "0" + day;
var dateString = currentYear.toString() + '-' + month + '-' + day + 'T00:00:00';
return new Date(dateString);
}
Solution 3:[3]
VisualBasic Excel VBA Code
Function Easter_Sunday(Year)
k = Int(Year / 100) 'the secular number
s = 2 - Int((3 * k + 3) / 4) 'the secular sun control
m = 15 + Int((3 * k + 3) / 4) - Int((8 * k + 13) / 25) 'the secular moon circuit
a = Year Mod 19 'the lunar parameter
d = (19 * a + m) Mod 30 'the seed for the first full moon in spring
r = Int(d / 29) + (Int(d / 28) - Int(d / 29)) * Int(a / 11) 'the calendar correction amount
EB = 21 + d - r 'the Easter border
FS = 7 - (Year + Int(Year / 4) + s) Mod 7 'the first Sunday in March
ED = 7 - (EB - FS) Mod 7 'Easter distance in days
ESM = EB + ED - 1 'the date of Easter Sunday as the March date
'--------------------------------------- "For Years (1900-9999)" ------------------------------
Easter_Sunday = DateValue(1 & "." & 3 & "." & Year) + ESM
'--------------------------------------- "or for all Years" -----------------------------------
Month_ = 3 + Int(ESM / 31) 'Month March or April
ES = 1 + ESM Mod 31 'Eastersunday
Easter_Sunday = ("Su, " & Year & "." & Format(Month_, "00") & "." & Format(ES, "00"))
End Function
Solution 4:[4]
...or someone needs it in ColdFusion:
<!---
Function to get the easter date adopted to ColdFusion as of
https://stackoverflow.com/questions/26022233/calculate-the-date-of-easter-sunday
--->
<cffunction name="getEasterDate">
<cfargument name="year" required="true">
<cfscript>
var currentYear = arguments["year"];
var a = floor(currentYear % 19);
var b = floor(currentYear / 100);
var c = floor(currentYear % 100);
var d = floor(b / 4);
var e = floor(b % 4);
var f = floor((b + 8) / 25);
var g = floor((b - f + 1) / 3);
var h = floor((19 * a + b - d - g + 15) % 30);
var i = floor(c / 4);
var k = floor(c % 4);
var l = floor((32 + 2 * e + 2 * i - h - k) % 7);
var m = floor((a + 11 * h + 22 * l) / 451);
var n = floor((h + l - 7 * m + 114) / 31);
var p = floor(((h + l - 7 * m + 114) % 31) + 1);
var month = n;
if (len(month) lt 2) {
month = "0" & month;
}
var day = p;
if (len(day) lt 2) {
day = "0" & day;
}
var dateString = day & '.' & month & '.' & currentYear;
return dateString;
</cfscript>
</cffunction>
Solution 5:[5]
/**
* Orthodox easter formula, invented back in 1800 by famous Carl Friedrich Gauss.
*/
public static LocalDate getEasterSundayDate(int year) {
int a = year % 19,
b = year / 100,
c = year % 100,
d = b / 4,
e = b % 4,
g = (8 * b + 13) / 25,
h = (19 * a + b - d - g + 15) % 30,
j = c / 4,
k = c % 4,
m = (a + 11 * h) / 319,
r = (2 * e + 2 * j - k - h + m + 32) % 7,
month = (h - m + r + 90) / 25,
day = (h - m + r + month + 19) % 32;
return LocalDate.of( year, month, day );
}
Solution 6:[6]
Kotlin version:
private fun getEasterSundayDate(year: Int): String {
val a = year % 19
val b = year /100
val c = year % 100
val d = b /4
val e = b % 4
val g = (8 * b + 13) / 25
val h = (19 * a + b - d - g + 15) % 30
val j = c / 4
val k = c % 4
val m = (a + 11 * h) / 319
val r = (2 * e + 2 * j - k - h + m + 32) % 7
val month = (h - m + r + 90) / 25
val day = (h - m + r + month + 19) % 32
return String.format("%02d", day) + "." + String.format("%02d", month)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | AppDreamer |
| Solution 3 | |
| Solution 4 | Fips |
| Solution 5 | dobrivoje |
| Solution 6 | Mika |
