CS303E Homework 2

Instructor: Dr. Bill Young
Due Date: Tuesday, January 30, 2024 at 11:59pm

Date of Easter Sunday

Easter Sunday is the first Sunday after the first full moon of spring. Write a program that computes the date of Easter Sunday for a specified year.

An algorithm to figure this out originally was invented by the mathematician Carl Friedrich Gauss. It has a lot of steps, but they are simple ones.

Hint: when it says to divide or asks for the quotient, that means integer division. And when it asks for the remainder, that means use the remainder operation. So the quotient of 19 divided by 3 is (19 // 3) with a remainder of (19 % 3). If you need both, you can do that in two steps.

  1. Ask the user for the year (such as 2001). Information on how to do that is below. Save the year in variable y.
  2. Divide y by 19 and call the remainder a. Ignore the quotient.
  3. Divide y by 100 to get a quotient b and a remainder c.
  4. Divide b by 4 to get a quotient d and a remainder e.
  5. Divide (8 * b + 13) by 25 to get a quotient g. Ignore the remainder.
  6. Divide (19 * a + b - d - g + 15) by 30 to get a remainder h. Ignore the quotient.
  7. Divide c by 4 to get a quotient j and a remainder k.
  8. Divide (a + 11 * h) by 319 to get a quotient m. Ignore the remainder.
  9. Divide (2 * e + 2 * j - k - h + m + 32) by 7 to get a remainder r. Ignore the quotient.
  10. Divide (h - m + r + 90) by 25 to get a quotient n. Ignore the remainder.
  11. Divide (h - m + r + n + 19) by 32 to get a remainder p. Ignore the quotient.
Then Easter Sunday falls on day p of the 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 
Hence in 2001, Easter Sunday was on April 15. (BTW: You might add print statements to print the intermediate results to see if you're on the right track. But remove them or comment them out before you submit. See the Programming Hints section below.)

How to ask for user input: In your program you'll need to prompt the user to enter the year and then write out the date for Easter Sunday. The input function is not covered until slideset 3, but it's pretty easy. To get the value of the year into variable y, include the following statement:

y = int( input("Enter year: ") )
When this statement is executed, it will print the prompt "Enter year: " on the screen, and wait for the user to enter a number. After the user types in a number, say 2001, this number will be read by the program as a string, say "2001". Then the function int() will convert that string back into an integer which will be stored in the variable y. From there you're good to go.

You can assume that the year entered is a positive integer. Your program doesn't have to check that and doesn't have to work if the user enters a bad value. We won't test your program on illegal values.

After you have computed p and n, you are ready to print out the result in the format illustrated below. Your session must look exactly like the following (assuming the user enters year 2001). Any deviations in the format of the output will result in points being deducted.

Enter year: 2001
In 2001 Easter Sunday is on month 4 and day 15
You can type "Easter 2001" (or any other year) into Google to verify that your program is giving you the correct result.

BTW: we're aware that the Julian calendar changed to the Gregorian calendar by some countries as early as 1582, but not by Britain and its colonies until 1752. I'm pretty sure that Gauss' algorithm only works for the Gregorian calendar. Don't worry about that. We won't test your code for any years when that would matter.

Turning in the Assignment:

The program should be in a file named EasterSunday.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment hw2 under the assignments sections by uploading your python file.

Your file must compile and run before submission. It must also contain a header with the following format:

# File: EasterSunday.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date:
# Description of Program: 

If you submit multiple times to Canvas, it will rename your file name to something like EasterSunday-1.py, EasterSunday-2.py, etc. Don't worry about that; we'll grade the latest version.

Programming Tips:

Many assignments this semester will include this section called Programming Tips. In general, these are not hard requirements about this specific assignment; they are suggestions to help you become better programmers. So read and apply them!

Naming Variables: It's typically considered lousy programming practice to use single letter variable names. It's almost always much better to use descriptive names. However, the variables in this problem don't have obvious individual meanings, so go ahead and just use the variable names given.

Also, it's always a good idea to comment any part of your code that would be unclear to someone reading it later. However, the individual steps of this algorithm are individually clear, but collectively murky. That is, it's very easy to see what each step does; it's much murkier how they work together to compute the date of Easter. But then again, I'm not nearly as smart as Gauss, who was one of the greatest mathematicians of all time. So there's no need to comment each individual step. But there needs to be an overall comment that says what the algorithm accomplishes.

Output format: As explained in slide set 1, there are multiple ways to run your program. If you run in interactive mode (in the Python loop), the system will automatically display the result of every command (unless the result is None). If that result is a string, it will display with string quotes ('answer'). If you're running the program in batch mode (from the command line, as e.g. python myProgram.py) or explicitly print a string, it won't appear with string quotes. If you run your program in batch mode the only things you'll see displayed are the things you explicitly print; it won't display the results of individual commands. The following is in interactive mode:

>>> string = "my string"
>>> string
'my string'
>>> print(string)
my string
>>> 
If your string output doesn't match what's shown in the assignment sample output because of the presence or absence of string quotes, that may be the reason. Usually, it's not an issue to worry about.

But remember, you must turn in a file that contains the code to do this computation. So it's not adequate to just run the steps in interactive mode. You can do that while you're debugging the steps; but you need a complete program stored in a file for your submission.

Debugging with print statements: Many times over the course of this semester, you'll be asking yourself or others: What is wrong with my program? A good way to figure that out is to add print statements. The current program for example, has 11 steps you have to implement. If you wait until you've completed all 11 and then get the wrong answer, it's pretty hard to figure out where you went wrong. But we showed you above, for one specific example, the value of every intermediate result. So if you print the intermediate results as you generate them, you can focus in on the spot where your computations went wrong. A statement like:

   print("a =", a)
right after you've computed a will tell you if you're on the right track. It's only a small amount of effort to add the print statements, but it may save you a lot of trouble later on. Note that it's often especially useful to print out the parameters of a function call to see if the values you thought were passed were actually the values passed. Of course, don't forget to comment out or remove the extra print statements before you submit your assignment. To comment out a statement, just add a # character at the front of the line.