vytvor v pythonu program pro vypocet velikonocnich nedeli dle juliánského kalendare. budou se používat pouze algoritmy pro vypocet velikonocnich nedeli dle juliánského kalendare. nesmí se používat zadne algoritmy pro vypocet velikonocnich nedeli z gregoriánského kalendare. rozsah roku pro vypocet bude 400-3000 dle bezne známého letopoctu. pak bude program umoznovat vytvaret dve linie kalendaru, které se od sebe lisi tim, ze se zaprvé na vstupu v libovolny rok zada počet dnu, který bude mit prvni z tech kalendaru v roce méně, ale pouze ten jeden rok, zadruhe ze se na vstupu zada libovolny sty rok, od kterého se budou povazovat nasledujici tri ste roky za roky s jednim dnem méně oproti druhemu kalendari a tak to bude pokracovat dal v rozmezích 400 let - prvni sty rok stejny, další tri ste roky o jeden den méně oproti druhemu kalendari. program pak vypocita kolik tydnu je rozdil mezi velikonocnimi nedelemi, vcetne rozdilu 0 tyden, pokud takova moznost nastane, v jednotlivých rocích od 0 po 6000 a tyto hodnoty označené roky zapise do textového souboru do stejne slozky, kde bude umisten python skript. dale bude v programu moznost zadat rozsah roku, za kterézto obdobi bude spocitan soucet odlisnosti v jednom roce.
Assistant
Certainly! Below is a Python program that calculates Julian Easter Sundays for years in the range 400 to 3000. It allows creating two calendar lines with specific modifications as per your requirements:
Calendar 1: Standard Julian calendar with the option to subtract one day from a specific year.
Calendar 2: Standard Julian calendar with a pattern where, starting from a specified century year, every subsequent 100-year period has the first three years with one day less.
The program then calculates the difference in weeks between the Easter Sundays of the two calendars for each year from 400 to 6000 and writes the results to a text file. Additionally, it allows you to specify a range of years to compute the total sum of differences.
Program Overview
Julian Easter Calculation: Implements the Julian algorithm to compute Easter Sunday for a given year.
Calendar Modifications:
Single Year Adjustment: Subtract one day from a specified year in Calendar 1.
Century Pattern Adjustment: Starting from a user-specified century year, subtract one day from the first three years of every subsequent 100-year period in Calendar 2.
Difference Calculation: Computes the difference in weeks between the two Easter Sundays.
Output: Writes the yearly differences to a text file and allows computing the sum of differences over a specified range.
Implementation
python
import datetime
from datetime import timedelta
import math
def julian_easter(year):
"""
Calculate Julian Easter for a given year.
Returns the date as a datetime.date object.
"""
a = year % 4
b = year % 7
c = year % 19
d = (19 * c + 15) % 30
e = (2 * a + 4 * b - d) % 7
month = (d + e + 114) // 31
day = ((d + e + 114) % 31) + 1
# Julian calendar to Gregorian for datetime compatibility
# The difference between Julian and Gregorian dates varies, but for simplicity, we'll treat them as same
return datetime.date(year, month, day)
def adjust_year_days(year, adjustment):
"""
Adjust the number of days in a year.
If adjustment is -1, subtract one day.
"""
# For simplicity, return a timedelta adjustment
return timedelta(days=adjustment)
def get_calendar_days_adjustments(modified_years):
"""
Create a dictionary with year as key and day adjustment as value.
"""
adjustments = {}
for year, adj in modified_years.items():
adjustments[year] = adj
return adjustments
def calculate_easter_difference(year, cal1_adjustments, cal2_adjustments):
"""
Calculate the difference in weeks between two Easter dates for a given year,
considering calendar adjustments.
"""
easter1 = julian_easter(year)
easter2 = julian_easter(year)
# Apply adjustments to Calendar 1
if year in cal1_adjustments:
easter1 += adjust_year_days(year, cal1_adjustments[year])
# Apply adjustments to Calendar 2
if year in cal2_adjustments:
easter2 += adjust_year_days(year, cal2_adjustments[year])
delta_days = abs((easter1 - easter2).days)
delta_weeks = delta_days // 7 # Integer division for full weeks
return delta_weeks
def generate_calendars(single_adjust_year=None, century_start=None):
"""
Generate adjustments for both calendars based on user input.
"""
cal1_adjustments = {}
cal2_adjustments = {}
# Calendar 1: Subtract one day from a specific year
if single_adjust_year:
cal1_adjustments[single_adjust_year] = -1
# Calendar 2: Subtract one day from the first three years of every 100-year period starting at century_start
if century_start:
for year in range(century_start, 3001, 100):
for offset in range(3):
target_year = year + offset
if 400 <= target_year <= 3000:
cal2_adjustments[target_year] = -1
return cal1_adjustments, cal2_adjustments
def write_differences_to_file(filename, differences):
"""
Write the differences dictionary to a text file.
"""
with open(filename, 'w') as file:
file.write("Year,Week Difference\n")
for year in sorted(differences.keys()):
file.write(f"{year},{differences[year]}\n")
def main():
print("Julian Calendar Easter Difference Calculator")
# User inputs for Calendar modifications
print("\n--- Calendar 1 Modification ---")
single_adjust = input("Do you want to subtract one day from a specific year in Calendar 1? (y/n): ").strip().lower()
single_adjust_year = None
if single_adjust == 'y':
while True:
try:
single_adjust_year = int(input("Enter the year to adjust (400-3000): "))
if 400 <= single_adjust_year <= 3000:
break
else:
print("Year must be between 400 and 3000.")
except ValueError:
print("Please enter a valid year.")
print("\n--- Calendar 2 Modification ---")
century_adjust = input("Do you want to apply century pattern adjustments to Calendar 2? (y/n): ").strip().lower()
century_start = None
if century_adjust == 'y':
while True:
try:
century_start = int(input("Enter the starting century year (e.g., 400, 500, ..., 2900): "))
if 400 <= century_start <= 3000 and century_start % 100 == 0:
break
else:
print("Century start year must be between 400 and 3000 and divisible by 100.")
except ValueError:
print("Please enter a valid century year (e.g., 400, 500, 600).")
# Generate calendar adjustments
cal1_adj, cal2_adj = generate_calendars(single_adjust_year, century_start)
# Calculate differences from year 400 to 6000
print("\nCalculating differences. Please wait...")
differences = {}
for year in range(400, 6001):
delta_weeks = calculate_easter_difference(year, cal1_adj, cal2_adj)
differences[year] = delta_weeks
# Write to file
output_filename = "easter_differences.txt"
write_differences_to_file(output_filename, differences)
print(f"Differences written to {output_filename}")
# Calculate sum of differences over a range
print("\n--- Sum of Differences Over a Range ---")
while True:
try:
start_year = int(input("Enter the start year (400-6000): "))
end_year = int(input("Enter the end year (400-6000): "))
if 400 <= start_year <= 6000 and 400 <= end_year <= 6000 and start_year <= end_year:
break
else:
print("Please enter valid years within the range 400 to 6000, and ensure start <= end.")
except ValueError:
print("Please enter valid years.")
sum_diff = sum(differences.get(year, 0) for year in range(start_year, end_year + 1))
print(f"The total sum of week differences from {start_year} to {end_year} is: {sum_diff}")
if __name__ == "__main__":
main()
Explanation of the Code
Julian Easter Calculation (julian_easter):
Implements the Julian algorithm to compute Easter Sunday.
Returns the date as a datetime.date object.
Note: The Julian calendar differs from the Gregorian calendar, especially in day alignment. For simplicity, this code treats them similarly, but for more accurate historical date alignment, additional adjustments are needed.
Adjusting Year Days (adjust_year_days):
Adjusts the number of days in a year by returning a timedelta object.
Computes the Easter date for both calendars, applies any day adjustments, and calculates the absolute difference in weeks.
Writing to File (write_differences_to_file):
Writes the year and corresponding week difference to a text file named easter_differences.txt.
Main Function (main):
Handles user interaction for specifying calendar modifications.
Generates the calendars based on user input.
Calculates the weekly differences for each year from 400 to 6000.
Writes the results to a text file.
Allows the user to specify a range of years to calculate the total sum of differences.
Usage Instructions
Run the Script:
Ensure you have Python installed on your system.
Save the script to a file named, for example, julian_easter_diff.py.
Open a terminal or command prompt and navigate to the directory containing the script.
Run the script using the command:
bash
python julian_easter_diff.py
Provide Inputs:
Calendar 1 Modification: Choose whether to subtract one day from a specific year. If yes, provide the year.
Calendar 2 Modification: Choose whether to apply century pattern adjustments. If yes, provide the starting century year (must be divisible by 100).
Sum of Differences: After the calculations, you can specify a range of years to get the total sum of week differences.
Output:
The program will create a text file named easter_differences.txt in the same directory, containing the year-wise week differences.
It will also display the total sum of differences for the specified range.
Notes and Considerations
Date Handling: The datetime module in Python uses the Gregorian calendar. The Julian calendar differs, especially in leap years and century rules. For precise historical computations, consider using specialized libraries like skyfield or astroPy that handle different calendar systems more accurately.
Year Range: The program handles years from 400 to 6000 as per your requirement. Adjust the range in the loop if you need a different range.
Adjustments: Currently, only subtraction of one day is implemented. If you need more complex adjustments (like adding days or handling multiple adjustments per year), you'll need to expand the adjust_year_days and related functions.
Error Handling: The program includes basic input validation. Ensure to provide valid inputs as prompted.
Feel free to modify and extend this script based on your specific needs!